我知道这已被回答,但我不满意给出的答案。Angular 4如何在注销后重定向页面?
我的问题很简单,用户想要注销。
如果它在需要登录的页面上(在auth.guard.ts中设置auth.service.ts)并且用户注销,我希望他重定向到主页。
但是如果他在安全页面我不想重定向他。
我可以使用AuthGuard,但我不想重新载入页面,所以没有:
location.reload()
什么是我的选择吗?
我知道这已被回答,但我不满意给出的答案。Angular 4如何在注销后重定向页面?
我的问题很简单,用户想要注销。
如果它在需要登录的页面上(在auth.guard.ts中设置auth.service.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 { Router } from '@angular/router';
@Injectable()
export class AuthService {
constructor(private _router: Router){}
authSignOut(){
localStorage.removeItem('user');
this._router.navigate(['home']);
}
}
其余的,我有点困惑......可以分享你的代码吗?
请找到下面的代码重定向希望它可以帮助
import {Component} from '@angular/core';
import {Router} from '@angular/router';
export class LogoutComponant {
title = 'Logout';
constructor(
private router: Router,
) {}
onLogout() {
this.router.navigate(['/'])
}
}
我不想重定向他,如果他是一个不要求进行登录页面上。假设我有三页/家庭/管理员和/内容。如果用户在/ admin上注销,我想将他重定向到/ home。如果他从内容注销,我不想重定向他。随着你的解决方案,我会将他重定向到任何地方。 – Frenentix
为authSignOutContent()创建一个函数,并且不重定向...或者传递一个参数给你的服务,当param为true时重定向..可以解决你的问题 –