【问题标题】:nav inside a promise function in ionic 2在 ionic 2 的 Promise 函数中导航
【发布时间】: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


    【解决方案1】:

    改变

    .then(function (data) {
    

    .then( (data) => {
    

    如果您使用function 而不是粗箭头语法,您的this 将不会引用该页面。

    我建议您阅读:https://stackoverflow.com/a/20279485/5706293

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 2017-05-02
      相关资源
      最近更新 更多