簡體   English   中英

Angular 4如何在注銷后重定向頁面?

[英]Angular 4 How to redirect page after logout?

我知道這已經得到了回答,但我對給出的答案並不滿意。

我的問題很簡單,用戶想要注銷。

如果它位於需要登錄的頁面上(使用auth.service.ts在auth.guard.ts中設置)並且用戶注銷,我希望將其重定向到主頁。

但是,如果他在一個安全的頁面上,我不想重定向他。

我可以使用AuthGuard,但我不想重新加載頁面,所以沒有:

location.reload()

我的選擇是什么?

import { Router } from '@angular/router';
.....

constructor(private router:Router, authService: AuthService){}
  //toastMessagesService: ToastMessagesService){} if you have one
....

onLogOut(){
  authService.logOut(); 

  if (this.router.url==="/admin"){
        //this.toastMessagesService.show("Logged out"); // display a toast style message if you have one :)
        this.router.navigate(["/home"]); //for the case 'the user logout I want him to be redirected to home.'
  }
  else{
   // Stay here or do something
   // for the case 'However if he is on a safe page I don't want to redirected him.'
   }
 }

請找到以下重定向代碼希望它有所幫助

 import {Component} from '@angular/core';
 import {Router} from '@angular/router';

  export class LogoutComponant {
    title = 'Logout';


constructor(

    private router: Router,

) {}

onLogout() {
    this.router.navigate(['/'])
}

}

你可以在他這樣登出家中時重定向用戶。

import { Router } from '@angular/router';

@Injectable()

export class AuthService {
    constructor( private _router: Router){}

    authSignOut(){
        localStorage.removeItem('user');
        this._router.navigate(['home']);
    }
}

關於其余的,我有點困惑......可以分享你的代碼嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM