4从历史记录中删除页面

4从历史记录中删除页面

本文介绍了Ionic 4从历史记录中删除页面(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

Android设备的菜单工具栏上有后退按钮。我要在登录应用程序并单击该后退按钮以在登录页面上进行路由时禁用此可能性。

Android devices has back button on menu toolbar. I want to disable the possibility when i login to my app and click on that back button to route on login page.

我想要的是,如果用户在登录后单击后退按钮,那么我将关闭该应用程序。
这是我下面的路由的初始代码。

I want if user click on back button after login then i close the app.Here is my initial code for routing below.

if (token) {
    this.router.navigate(['/main-tabs/tabs/dashboard'])
} else {
    this.router.navigate(['/login']).then();
}


推荐答案

我尝试了许多其他方法答案,但它们都不对我有用。但这是可行的:

I've tried many other answers but none of them really works for me. But this one works :

要在注销后禁止登录从返回到经过身份验证的页面,只需在中执行类似的操作app-routing.module.ts

To disallow the login from going 'back' to the authenticated page after logged out, just do something like this in your app-routing.module.ts :

{
    path: 'home',
    loadChildren: './home/home.module#HomePageModule',
    canActivate: [LoggedAuthGuard]
}

相反(防止使用后退按钮返回登录页面):

The same for the opposite (to prevent going back into login page with back button) :

{
    path: 'login',
    loadChildren: './login/login.module#LoginPageModule',
    canActivate: [NotLoggedAuthGuard]
}

以及 LoggedAuthGuard NotLoggedAuthGuard 必须实现 CanActivate 。示例代码如下(带有 Promise ,但它也适用于布尔返回):

And both LoggedAuthGuard and NotLoggedAuthGuard must implement CanActivate. Sample code as below (with Promise, but it also works with boolean return) :

import { Injectable } from '@angular/core';
import {CanActivate} from "@angular/router";
import {Storage} from "@ionic/storage";

@Injectable({
  providedIn: 'root'
})
export class LoggedAuthGuard implements CanActivate {
  constructor(protected storage: Storage) { }

  async canActivate() {
      return (await !!this.storage.get('access_token'));
  }
}

对于 NotLoggedAuthGuard 您只返回 LoggedAuthGuard 的反面。

async canActivate() {
    return (await !this.storage.get('access_token'));
}

希望这会有所帮助。

这篇关于Ionic 4从历史记录中删除页面(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:59