【发布时间】:2016-12-30 18:08:07
【问题描述】:
我在一个新的应用程序中使用 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.
【问题讨论】:
标签: javascript angular typescript ionic2