Ionic 4 Delete Page from History (Android)

本秂侑毒 提交于 2019-12-22 12:42:19

问题


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();
}

回答1:


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

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]
}

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'));
  }
}

For the NotLoggedAuthGuard you just returns the opposite of LoggedAuthGuard.

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

Hope this helps.




回答2:


What I understood from your question is after user login, You don't want to navigate to login page if back button is clicked. If I understood your question correctly you can try below solution.

one approach is changing root page

this.navCtrl.setRoot(HomePage);

or

You can achieve this by removing page from stack after successful transition. Place below code inside Login success method

let currentIndex = this.navCtrl.getActive().index;
this.navCtrl.push(DestinationPage).then(() => {
    this.navCtrl.remove(currentIndex);
});

Hope this helps you.




回答3:


This answer provides a solution for removing the login page from the browser's history by replacing it with a page, that the user was navigated to after successful login. It might be a good and quick solution to:

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




回答4:


I think you can do like that :

  this.platform.backButton.subscribe((()=>{

      if(this.router.url == <insertpathhome>) 
      {
        this.platform.exitApp();
      }
      else{
        //go back
      }
    });  


来源:https://stackoverflow.com/questions/55181141/ionic-4-delete-page-from-history-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!