问题描述
我花了好几个小时在我的项目中一直在寻找解决这个问题的方法无济于事。我已经阅读了许多其他无法读取属性......未定义的帖子,但似乎找不到我的解决方案。
For hours I've been searching for a solution to this problem in my project to no avail. I've read many of the other "Cannot read property ... of undefined" posts, but can't seem to find a solution for mine.
以下是我的项目中的相关代码。
Below is the relevant code in my project.
这是一个Ionic 2 / Apache Cordova项目,下面的页面是应用程序的登录页面。它不是核心应用程序组件之一,它只是一个常规页面。
This is an Ionic 2 / Apache Cordova project and the page below is the Sign In Page for the app. It is not one of the core app components, it is simply a regular page.
出于某种原因,在onSignIn()方法中没有识别NavController,但是我不知道为什么。我在构造函数中注入了NavController,看起来我没有遵循我所知道的任何已知程序,但它仍然每次都失败。
For some reason, the NavController is not being recognized in the onSignIn() method, but I have no idea why. I've injected NavController in the constructor, and it doesn't look like I am failing to follow any known procedure that I am aware of, but it's still failing every time.
import firebase from 'firebase';
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { HomePage } from './../home/home';
var provider = new firebase.auth.FacebookAuthProvider();
@Component({
selector: 'page-signin',
templateUrl: 'signin.html',
})
export class SignInPage {
constructor(public navCtrl: NavController, public params: NavParams) {}
onSignIn() {
firebase.auth().signInWithPopup(provider).then(function(result) {
console.log(result.credential.accessToken);
console.log(result.user.displayName);
this.navCtrl.setRoot(HomePage);
}).catch(function(error) {
console.log(error.message);
});
}
}
推荐答案
A解决此问题的更好方法是使用:
A better way to solve this issue would be by using arrow functions:
所以只需更改<$ c $即可解决您的问题c> onSignIn 这样的方法:
So your issue would be solved by just changing the onSignIn
method like this:
onSignIn() {
firebase.auth().signInWithPopup(provider).then((result) => {
console.log(result.credential.accessToken);
console.log(result.user.displayName);
this.navCtrl.setRoot(HomePage);
}).catch(function(error) {
console.log(error.message);
});
}
注意(结果)=> {...}
而不是函数(结果){...}
这篇关于无法读取未定义的属性'navCtrl'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!