angular - 我想从承诺值中获取价值,然后返回 canActive 布尔值
问题描述
我试图从值中评估将路由器保护与路径一起使用的值
我尝试使用从承诺中获取值到canActivate()
路由器保护中。
private routeURL = new Subject<string> ();
canActivate() {
if (this.authenticationService.currentUserValue) {
this.getNavigationEnd().then(res => console.log('2 ' + res) );
// I want to get this.getNavigationEnd()'s result[true or false] for router guard's canActivate().
return true;
}
this.router.navigate(['/401']);
return false;
}
async getNavigationEnd() {
this.router.events.pipe(
filter(event => event instanceof NavigationEnd)
).subscribe((result) => {
let value: any;
value = result;
this.routeURL.next(value.url);
});
const valueArray = this.routeURL.asObservable();
let bool = false;
await valueArray.subscribe(value => {
console.log(value);
if (this.menuURIarray.includes(value)) {
bool = true;
} else {
bool = false;
}
});
console.log('1 ' + bool);
return bool;
}
如果可以从带有值的 promise 中获取,则路由器守卫可以使用路由器路径检查路径。
this.menuURIarray
是菜单列表数组。它与路由器的 navigationEnd 路径进行比较。
然后如果这个数组和navigationEnd不一样,就返回true或者false。
然后 canActivate 使用它进行检查。这是我的路由器保护方案
解决方案
canActivate
可以同时返回boolean
, Promise<boolean>
, Observable<boolean>
. 在您的情况下canActivate
,可以只返回一个Observable或Promise。
此示例过滤NavigationEnd并返回true
。
canActivate() {
return this.router.events.pipe(
filter(event => event instanceof NavigationEnd),
map((event: NavigationEnd) => true),
)
}
文档:canActivate
推荐阅读
- python - 使用pickle保存/加载数据类对象
- vhdl - 如何在不从 Basysy3 FPGA 接收多个输入的情况下正确地将有限状态机实现到 VHDL 中
- wordpress - 在 WooCommerce 中按产品 ID 显示带有简码的产品价格
- python - Python & Pandas:构造 lambda 参数
- firebase - 带有 nginx 代理服务器的 Firebase 自定义域“需要设置”
- flutter - 如何在颤动中获取列表的值表单字段?我需要颤振表单字段的帮助
- powerbi - DAX过滤器功能不过滤
- reactjs - 反应:成功认证后重定向到 PrivateRoute 不起作用
- python - Heroku 网络中的 H14
- facebook - 是否可以在 facebook 墙上删除后调用 webhook?