如何在 Ionic 4 或 Angular 7 中清除历史记录或删除历史记录中的页面或组件



我正在 Ionic 4 中创建一个注销页面,我想要的是清除路由器历史记录,以便后退按钮不起作用,另外我想知道如何在导航到登录屏幕或导航到登录页面之前销毁注销页面。

我尝试在 Construction 函数中运行代码以清除本地存储,并且第一次一切正常,但第二次它没有运行该代码。据我了解,该页面已经加载,因此不会再次运行构造函数。我尝试将我的代码放在 ngDoCheck() 上,但它多次触发,我不想要。

我想清除

路由历史记录,以便在注销后按钮不起作用,此外,我想知道如何清除或删除我以前访问过的页面。

您无法阻止用户单击返回或删除其历史记录,但您可以在登录页面中添加保护,以便在用户登录后重定向用户。它应该像这样

保护文件

@Injectable({
  providedIn: 'root',
})
export class AuthGuard implements CanActivate {
  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot) {
    if(isUserLoggedIn()){ // this redirects the user
       this.router.navigate(['/logged-in-users-area']);
    }
    return isUserLoggedIn(); // this prevents him from reaching the login page
  }
}

路由文件

const routes: Routes = [{
    path: 'login',
    component: MyLoginComponent,
    canActivate: [AuthGuard],
  }]

注销时使用window.location.reload

window.location.reload();

最新更新