【问题标题】:Page Navigation Issues ionic2 and angular2页面导航问题 ionic2 和 angular2
【发布时间】:2017-11-03 23:08:39
【问题描述】:

在我的 Ionic 2 应用程序中,有两个页面,但在 ProfilePage 加载后,它会推送 LoginPage,然后刷新它并且页面变为空白。有谁知道为什么会这样?

这是我的代码

app.component.ts

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from 'ionic-native';

import { ProfilePage } from '../pages/profile/profile';
import { LoginPage } from '../pages/login/login';
import { AuthService } from '../services/auth/auth.service';

@Component({
  template: `<ion-nav [root]="rootPage"></ion-nav>`
 })
 export class AuthApp {
   rootPage: any = ProfilePage;

   constructor(platform: Platform, private auth: AuthService) {
     platform.ready().then(() => {       
       StatusBar.styleDefault();
       auth.startupTokenRefresh();
     });
   }
 }

profile.ts

 import {Component} from '@angular/core';
 import {AuthService} from '../../services/auth/auth.service';
 import { NavController } from 'ionic-angular';
 import { LoginPage } from '../login/login';

 @Component({
   templateUrl: 'profile.html',
 })
 export class ProfilePage {
   constructor(public auth: AuthService, public navCtrl: NavController) {

   }

   ngOnInit() {
    console.log(this.auth.authenticated())
    if (!this.auth.authenticated()) {
        this.navCtrl.setRoot(LoginPage);
    }
   }
 }

login.ts

 import { Component } from '@angular/core';
 import { NavController } from 'ionic-angular';
 @Component({
   templateUrl: 'login.html',
 })
 export class LoginPage {
    authType: string = "login";
    error: string;

   constructor(public navCtrl: NavController) {}
 }

关于我做错了什么有什么建议吗?为什么LoginPage推送时会刷新?

【问题讨论】:

  • 控制台有什么东西吗?
  • 控制台中没有任何内容

标签: angular ionic2


【解决方案1】:

可能是因为您将页面设置为 root 而不是推送它。而不是this.navCtrl.setRoot(LoginPage);,你可以试试this.navCtrl.push(LoginPage);

【讨论】:

    【解决方案2】:

    ngOnInit() 中的 console.log 语句会打印两次吗?

    ngOnInit() 可能会被调用两次:一次是在您声明页面时,另一次是在您推送页面时。

    您可以尝试将ngOnInit() 更改为ionViewDidLoad()(请参阅this resource 了解更多信息)

    无论如何,如果您能发布app.module.ts 文件的内容,那将会很有帮助。为什么你的页面类中没有 @IonicPage 装饰器?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-31
      • 2016-11-15
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 2019-07-21
      相关资源
      最近更新 更多