簡體   English   中英

應用程序啟動后,Ionic 2重定向到另一個頁面

[英]Ionic 2 redirect to another page after app boot

我想在localStorage上已經有令牌時將頁面重定向到主頁(覆蓋登錄頁面)。 怎么做? 我在app.component.ts上的構造函數()上有以下代碼,但它在請求完成之前首先顯示登錄

statusBar.backgroundColorByHexString('#D32F2F');
      splashScreen.hide();
      if(localStorage.getItem('token')){
        authProvider.silent_login().subscribe(res => {
          console.log(res);
          if(res.error==0){
            this.rootPage = HomePage;
          }
        })
      }

你能不能

    @ViewChild(Nav) nav: Nav;
    rootPage: any = null; // Initialize it as null
    pages: Array<{title: string, component: any}>;

    constructor(public platform: Platform, 
                public statusBar: StatusBar, 
                public splashScreen: SplashScreen,
                public commonProvider: CommonProvider) {

        this.commonProvider.retrieve("is_login").then(loggedIn => {
            // Assign the right page after checking the status
            this.rootPage = loggedIn ? TabsPage : SigninPage;
        });
     }

解決方案是生成一個名為splash的頁面並將app的根頁面設置為它,在構造函數中,我們檢查令牌。 如果失敗則將根頁面設置為登錄,如果成功將根頁面設置為主頁。

我認為這是唯一的解決方案。 啟動頁面作為憑據檢查器。

也許它有助於其他人

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM