如何在ionic 4中有条件地设置根页?

时间:2019-05-21 07:28:21

标签: android angular cordova ionic-framework ionic4

我正在从事离子4的应用。当用户登录时,我添加了一个复选框,要求您记住我,因此,下次用户打开该应用程序时,它不会显示登录页面,而是将用户直接转到首页。我有Google,找到了this。但是,在使用公认的解决方案时,我遇到了一个问题,该问题显示登录页面2到3秒钟,然后打开“主页”。我该如何安全地实现它而没有拖延?

app.component.ts

import { SmsVerificationService } from 'src/app/services/SMS/sms-verification.service';
    import { Component } from '@angular/core';
    import { Platform } from '@ionic/angular';
    import { SplashScreen } from '@ionic-native/splash-screen/ngx';
    import { StatusBar } from '@ionic-native/status-bar/ngx';
    import { FCM } from '@ionic-native/fcm/ngx';


    import { Plugins, Capacitor } from '@capacitor/core';
    import { Router } from '@angular/router';
    import { Storage } from '@ionic/storage';
    @Component({
      selector: 'app-root',
      templateUrl: 'app.component.html'
    })
    export class AppComponent {
      constructor(
        private platform: Platform,
        private splashScreen: SplashScreen,
        private statusBar: StatusBar,
        private fcm: FCM,
        private route: Router,
        private storage: Storage

      ) {



        this.initializeApp();
      }

      initializeApp() {

        this.platform.ready().then(() => {
          this.fcm.getToken().then(token => {
            console.log(' token is ', token);
          });
          this.fcm.onTokenRefresh().subscribe(token => {
            console.log('on token refresh ', token);
          });
          this.fcm.onNotification().subscribe(data => {
            console.log(data);
            if (data.wasTapped) {
              console.log('Received in background');
              // this.router.navigate([data.landing_page, data.price]);
            } else {
              console.log('Received in foreground');
              // this.router.navigate([data.landing_page, data.price]);
            }

          });
          this.storage.get('isLogined').then(data => {
                    if (data)
              this.route.navigateByUrl('/main-tab');
          })
          this.statusBar.styleDefault();
          this.splashScreen.hide();
          if (Capacitor.isPluginAvailable('SplashScreen')) {
            Plugins.SplashScreen.hide();
          }
        });
      }
    }

应该用于更改页面的代码

this.storage.get('isLogined').then(data => {
                    if (data)
              this.route.navigateByUrl('/main-tab');
          })

1 个答案:

答案 0 :(得分:1)

您是否有单独的登录组件?如果是这样,您可以在选中记住我后创建guard并将其添加到登录组件中,以导航到主页。