Angular this.router.navigate在将来的保护块路由中
我在Angular 4中设置了两个防护——一个在用户试图到达受保护的路由时将用户重定向到登录页面,另一个在用户还没有到达“主页”时将用户重定向到欢迎页面 卫兵们自己的工作非常出色。。。但我注意到一些非常奇怪的行为。通过WelcomeTraveler guard中的Angular this.router.navigate在将来的保护块路由中,angular,angular-router,angular-router-guards,Angular,Angular Router,Angular Router Guards,我在Angular 4中设置了两个防护——一个在用户试图到达受保护的路由时将用户重定向到登录页面,另一个在用户还没有到达“主页”时将用户重定向到欢迎页面 卫兵们自己的工作非常出色。。。但我注意到一些非常奇怪的行为。通过WelcomeTraveler guard中的this.router.navigate添加重定向会使应用处于无法从第一个guard访问受保护路由的状态,即使在登录后也是如此。我只是一直被送回主页 这是我的警卫: export class AuthGuardLoggedInUser
this.router.navigate
添加重定向会使应用处于无法从第一个guard访问受保护路由的状态,即使在登录后也是如此。我只是一直被送回主页
这是我的警卫:
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
}
}
在WelcomeTraveler
guard中出现this.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();
}
}
导出类LoginPageComponent实现OnDestroy{
私有订阅:订阅;
构造函数(专用存储:存储,专用路由器:路由器){
此.\u重定向订阅=\u存储。选择((状态)=>state.AuthNState)。订阅((auth)=>
{
如果(授权连接)此路由器导航(['');
})
}
恩贡德斯特罗(){
//在实例被销毁之前调用一次。
//将“implements OnDestroy”添加到类中。
此._重定向订阅。取消订阅();
}
}
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();
}
}