2016-12-30 153 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. 

回答