我知道这已经得到了回答,但我对给出的答案并不满意.
我的问题很简单,用户想要注销.
如果它位于需要登录的页面上(使用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.' } }