2016-12-30 158 views
2

我使用ionic2在新的应用程序,我想路由调用来自服务器的HTTP请求承诺函数内我的网页,导航离子2

一个承诺函数里面我要访问的Navcontroller在promise函数内部,但Navcontroller不能从promise函数内部访问。 承诺的功能是的AuthenticationService
内部功能的承诺,这是我的里面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. 

回答