Asynchronous 如何处理路线警卫和;ionic 4中的异步ionic.storage?
我想使用Guard异步获取存储在异步本地存储中的用户令牌。我尝试了以下几点:Asynchronous 如何处理路线警卫和;ionic 4中的异步ionic.storage?,asynchronous,routing,jwt,ionic4,guard,Asynchronous,Routing,Jwt,Ionic4,Guard,我想使用Guard异步获取存储在异步本地存储中的用户令牌。我尝试了以下几点: canActivate(): boolean { return this.lsProvider .getValue(Constants.KEY_USER_TOKEN) .map(value => value != null) .first(); 我也尝试了使用subscribe的方法,但没有成功: this.lsProvider .get
canActivate(): boolean {
return this.lsProvider
.getValue(Constants.KEY_USER_TOKEN)
.map(value => value != null)
.first();
我也尝试了使用subscribe的方法,但没有成功:
this.lsProvider
.getValue(Constants.KEY_USER_TOKEN).subscribe(isSuccess => {
if (isSuccess) {
return true;
}
else{
return false;
}
}
助手方法正在跟踪并返回承诺
getValue(key: string) {
console.log("getValue " + key);
return this.storage.get(key);
}
在这种情况下,我希望避免使用事件广播。我应该使用async/await还是其他什么?任何示例都值得欣赏。来自的canActivate
接口告诉我们返回类型可以是可观察的、承诺的,也可以是布尔类型(除其他外):
不幸的是,这个例子不起作用。我被重定向到根URL(localhost:8100)。如果我试图从canActivate强制返回false,我将成功重定向到登录。@redrom重定向到根url是什么意思?防护仅决定是否可以加载分配给它的页面。您在哪里尝试重定向用户?
interface CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
}
public canActivate(): Promise<boolean> {
return this.lsProvider
.getValue(Constants.KEY_USER_TOKEN)
.then(value => value != null)
}