gpt4 book ai didi

angular - Guard 中的 this.router.navigate 将来会阻止路由

转载 作者:太空狗 更新时间:2023-10-29 17:56:56 25 4
gpt4 key购买 nike

我在 Angular 4 中设置了两个守卫——一个在用户尝试访问 protected 路由时将用户重定向到登录页面,另一个在用户尚未访问时将用户从“主页”重定向到欢迎页面还没有。

守卫本身工作得很好......但我注意到一些非常奇怪的行为。通过 WelcomeTraveler 守卫中的 this.router.navigate 添加重定向使应用程序处于我无法从 first 守卫访问 protected 路由的状态,即使在登录后也是如此.我总是被送回主页。

这是我的守卫:

export class AuthGuardLoggedInUser implements CanActivate {
private isLoggedIn: boolean;
private working: boolean;
constructor (@Inject(Store) private _store:Store<AppStore>, @Inject(Router) private _router: Router)
{
_store.select(state => state.AuthNState).subscribe(auth =>
{
this.isLoggedIn = auth.connected
this.working = auth.working
})
}
canActivate() {
if (this.working)
{
let promise: Promise<boolean> = new Promise((resolve, reject) => {
let sub = this._store.select(state => state.AuthNState).subscribe(auth =>
{
if (!auth.working) {
resolve(auth.connected)
sub.unsubscribe()
if (!auth.connected) this._router.navigate(['/i/login']);
}
})
});
return promise
}
else if (this.isLoggedIn){
return true
}
else {
this._router.navigate(['/i/login']);
}

export class WelcomeTraveler implements CanActivate {
private hasAlreadyVisitedWelcomePage: boolean;
private isLoggedIn: boolean;
private working: boolean;
constructor (@Inject(Store) private _store:Store<AppStore>, @Inject(Router) private _router: Router)
{
_store.select(state => state.AuthNState).subscribe(auth =>
{
this.isLoggedIn = auth.connected
this.working = auth.working
})
}
canActivate() {
if (this.working)
{
let promise: Promise<boolean> = new Promise((resolve, reject) => {
let sub = this._store.select(state => state.AuthNState).subscribe(auth =>
{
if (!auth.working) {
resolve(auth.connected)
sub.unsubscribe()
this.hasAlreadyVisitedWelcomePage = true
this._router.navigate(['/i/welcome']);
}
})
});
return promise
}
else if (this.isLoggedIn){
return true
}
else if (!this.hasAlreadyVisitedWelcomePage){
this.hasAlreadyVisitedWelcomePage = true
this._router.navigate(['/i/welcome']);
}
else return true
}
}

这是路由表的片段:

export var AppRoutes = RouterModule.forRoot([
{
path: '',
component: HomeComponent,
canActivate: [WelcomeTraveler]
}, {
path: 'i/getstarted',
component: GetStartedPageComponent,
canActivate: [AuthGuardLoggedInUser]
}, {
path: 'i/login',
component: LoginPageComponent
}, {
path: 'i/profile',
component: ProfilePageComponent,
canActivate: [AuthGuardLoggedInUser]
}, {
path: 'i/welcome',
component: WelcomePageComponent
}])

WelcomeTraveler 守卫中 this.router.navigate 的存在似乎导致了问题,即使这些行从未被命中!登录后,我会在尝试路由到个人资料(成功通过第一个守卫后)后立即被送回“主页”。如果我删除导航线 - 问题就会消失。

有什么想法吗?

最佳答案

正如经常发生的那样,我走错了路。对于那些可能已经加星标或赞成这个的人,我建议你检查你可能拥有的任何调用 router.navigate 的订阅——在我的例子中,我没有清理这些订阅在我的登录/注册组件上...所以一旦订阅被初始化,只要状态更新,我就会被重定向到主页。

像这样固定:

export class LoginPageComponent implements OnDestroy {
private _redirectSubscription: Subscription;
constructor (private _store:Store<AppStore>, private router: Router) {
this._redirectSubscription = _store.select((state) => state.AuthNState).subscribe((auth) =>
{
if (auth.connected) this.router.navigate(['']);
})
}

ngOnDestroy() {
//Called once, before the instance is destroyed.
//Add 'implements OnDestroy' to the class.
this._redirectSubscription.unsubscribe();
}
}

关于angular - Guard 中的 this.router.navigate 将来会阻止路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46476934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com