【问题标题】:How to conditionally set root page in ionic 4?如何有条件地在 ionic 4 中设置根页面?
【发布时间】:2019-05-21 07:28:20
【问题描述】:

我正在开发 ionic 4 应用程序。当用户登录时,我添加了一个复选框来询问记住我,因此下次用户打开应用程序时它不会显示登录页面并直接将用户转发到主页。我有谷歌并找到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');
          })

【问题讨论】:

    标签: android angular cordova ionic-framework ionic4


    【解决方案1】:

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

    【讨论】:

    • 是的,我有一个单独的登录组件,并且在登录中使用保护会导致问题,它会显示登录页面几秒钟。@Dániel Barta
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多