【发布时间】:2018-02-12 22:02:27
【问题描述】:
- 我有一个使用两个保护的 Angular 设置。
canLoad和canActivate - 两者都通过
@select从@angular-redux/store 获取相同的observable
问题:为什么canActivate 与@select 返回的observable 一起工作,而canLoad 从那时起会中断所有路由?这两个守卫有什么区别?
相关角度问题:https://github.com/angular/angular/issues/18991
auth.guard.ts
@Injectable()
export class AuthGuard implements CanLoad, CanActivate {
@select() readonly authenticated$: Observable<boolean>; // @angular-redux/store
canLoad(): Observable<boolean> | boolean {
// return true; // works
return this.authenticated$; // ERROR: all routing stops from and to the current page
}
canActivate(): Observable<boolean> | boolean {
// return true; // works
return this.authenticated$; // works
}
}
app-routing.module
const routes: Routes = [
{
path: '',
component: SomeAComponent,
pathMatch: 'full'
},
{
path: 'someb',
component: SomeBComponent,
canActivate: [
AuthGuard
],
},
{
path: 'lazy',
loadChildren: './lazy/lazy.module#LazyModule',
canLoad: [
AuthGuard
]
},
{
path: '**',
redirectTo: '/'
}
];
【问题讨论】:
-
当
canLoad()保护用于组件而不是延迟加载模块时,它是否有效?我相信它与不加载canLoad路由的 RouterPreloader 相关联。来自router_preloader.d.ts文件:` * 如果路由受到canLoad守卫的保护,则预加载将不会加载它。 ` -
我以为
canLoad()只用于延迟加载模块?在已经加载的模块上使用它时你会期望什么? -
如 github 问题中所述,应完成 observable。我不确定
@select() readonly authenticated$.是否完成。如果不是,take(1)理论上不可能是我要找的东西,因为它不会让我从商店 (rxmarbles.com/#take) 获得最新更新。 -
有道理,我并没有真正使用
canLoad(),但这似乎有点奇怪 -
你找到解决这个问题的方法了吗??我刚刚遇到了守卫工作正常的问题,但说当我在守卫路线时没有配置商店
标签: javascript angular redux observable