防止按下按钮Ionic2

时间:2016-12-23 08:31:50

标签: angular ionic2 typescript2.0

实际上我正在Ionic2中创建一个应用程序,我在其中将我的根页面设置为登录页面,用户在其中添加了凭据,并且成功登录后使用setRoot重定向到另一个页面,例如Page1。

问题是,在移动应用程序中从应用程序按下按钮关闭,用户再次被强制登录屏幕,但我想要设置其会话。

任何人都可以提出这个问题以及如何克服这个问题。

我已经将ionic2与菜单一起用作启动。

2 个答案:

答案 0 :(得分:2)

之前我遇到过这个问题,我的解决方案是在服务中设置一个LoggedIn状态,我会从我的后卫调用我的登录组件,重定向到主要部分该应用程序,如果我已经登录。

示例:

这个方法调用我的ASP.NET控制器来获取Session参数(事先由login方法设置)以了解用户是否已经过身份验证,然后将相应的值设置为我的服务boolean( this.isLoggedIn)。

<强> Auth.service.ts

setupLoggedInState() {
    // HTTP Request to API
    let lControllerAction: string = "/IsConnectedState";
    let lControllerFullURL: string = this.controllerURL + lControllerAction;
    let headers = new Headers({ 'Content-Type': 'application/json' });
    // Empty body (necessary for this specific call)
    let options = new RequestOptions({ headers: headers, body: "" });
    // Call ASP Controller
    return this.http.get(lControllerFullURL, options)
        .map((res: any) => {
            let data = res.json();
            if (data == true) {
                this.isLoggedIn = true;
            }
        }
        ).catch(this.handleError);
}

这是一个让我的警卫工作的例子。我还有一些特定的代码来管理用户是否会手动输入URL,因此您不需要所有这些。

<强> Auth.guard.ts

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
    // Set IsLoggedIn if user has been authenticated before
    return this.authService.setupLoggedInState()
        .map(loggedIn => {
            // Check result
            if (this.authService.isLoggedIn) {
                // Managing redirection as well as manual URL input
                if (state.url === '/login') {
                    this.router.navigate(['/master/accueil']);
                }
                return true;
            } else {
                // Allow routing to LoginComponent if not logged in
                if (state.url === '/login')
                    return true;
                // Refuse access to specific URL => Redirect to Login
                this.router.navigate(['/login']);
                return false;
            }
        });
}

我是这样做的,因为我可以使用Session变量保存状态服务器端。如果要实现此行为,则需要找到自己的方法来保存此状态。 希望这有帮助

答案 1 :(得分:0)

您可以显示弹出窗口以确认注销。

import { Platform, IonicApp } from 'ionic-angular';

  constructor(public platform: Platform, private ionicApp: IonicApp){}

  initializeApp() {
    this.platform.ready().then(() => {
      //back button handle
      this.platform.registerBackButtonAction(() => {
        let ready = true;
        let activePortal = this.ionicApp._loadingPortal.getActive() ||
          this.ionicApp._modalPortal.getActive() ||
          this.ionicApp._toastPortal.getActive() ||
          this.ionicApp._overlayPortal.getActive();

        if (activePortal) { 
          //If any popup is open, dismiss it.
          ready = false;
          activePortal.dismiss();
          activePortal.onDidDismiss(() => { ready = true; });
          return;
        }
        else {
          if (this.nav.canGoBack()) {
            //You can come to parent screen by pressing back button.
            this.nav.pop();
          } else {
            //After coming back to home screen, you can ask confirmation for logout to user.
            this.confirmLogout();
          }
        }
      });
    });
  }

  confirmLogout() {
    let confirm = this.alertCtrl.create({
      title: 'Log out?',
      message: 'Do you want to log out?',
      buttons: [
        {
          text: 'No',
          handler: () => {
            console.log('No clicked');
          }
        },
        {
          text: 'Yes',
          handler: () => {
            navigator['app'].exitApp();
          }
        }
      ]
    });
    confirm.present();
  }
相关问题