我在一个新应用程序中使用 ionic2,并且我想将我的页面路由到一个从服务器调用 http 请求的 Promise 函数内,
我想访问 Promise 函数内的 Navcontroller,但无法从 Promise 函数内部访问 Navcontroller。
Promise 函数是 AuthenticationService 内部函数的 Promise
这是我在 app.component.ts 中的代码:
import {Component, ViewChild, Inject} from '@angular/core';
import {Platform, NavController} from 'ionic-angular';
import { StatusBar } from 'ionic-native';
import { HomePage } from '../pages/home/home';
import { LoginPage } from '../pages/login/login';
import {Authentication} from '../providers/authentication';
@Component({
template: `<ion-nav #myNav [root]="rootPage"></ion-nav>`,
providers:[Authentication],
})
export class MyApp {
@ViewChild('myNav') public nav : NavController
//rootPage = LoginPage;
constructor(platform: Platform, private auth:Authentication) {
platform.ready().then(() => {
StatusBar.styleDefault();
});
}
ngOnInit() {
this.auth.index()
.then(function (data) {
if(data['flag']==1)
{
this.nav.setRoot(HomePage);
}
else
{
this.nav.setRoot(LoginPage);
}
})
这是错误:
EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'nav' of null
TypeError: Cannot read property 'nav' of null.
最佳答案
改变
.then(function (data) {
到
.then( (data) => {
如果您使用 function
而不是粗箭头语法,您的 this
将不会引用该页面。
关于javascript - ionic 2 中 Promise 函数内的 nav,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41400671/