如何获得对 canActivate 进行验证的承诺的价值?
how can I get the value of a promise to do the validation on the canActivate?
我需要获取 returns this.isJwtValid()
的值,但目前它没有返回 promise
的结果值,代码继续其流程而不会停在那里,我需要在这一行中得到这个 promise
的结果:
let token = this.isJwtValid() //I need get the value of the promise in this line
继续我的逻辑。
我该怎么做?
这是我的代码:
export class verificarToken implements CanActivate {
constructor(private router: Router, private storage: Storage) {}
async isJwtValid() {
const jwtToken: any = await this.storage.get("token");
console.log(jwtToken); /// this is showed second
if (jwtToken) {
try {
return JSON.parse(atob(jwtToken.split(".")[1]));
} catch (e) {
return false;
}
}
return false;
}
canActivate(ruta: ActivatedRouteSnapshot, estado: RouterStateSnapshot) {
let token = this.isJwtValid(); //I need get the value of token here
if(token) {
console.log(token) // this is showed first
if (ruta.routeConfig.path == "login") {
this.router.navigate(["/tabs/tab1"]);
}
return true;
}
this.storage.clear();
this.router.navigate(["/login"]);
return false;
}
}
CanActivate 可以 returns 一个 Promise 或 Observable 或 Value,
所以你可以这样做。
canActivate(ruta: ActivatedRouteSnapshot, estado: RouterStateSnapshot) {
return this.isJwtValid().then(token => {
if (token) {
console.log(token) // this is showed first
if (ruta.routeConfig.path == "login") {
this.router.navigate(["/tabs/tab1"]);
return true;
}
this.storage.clear();
this.router.navigate(["/login"]);
return false;
});
}
}
canActivate 也可以 return 承诺。因此利用 async/await.
async canActivate(ruta: ActivatedRouteSnapshot, estado: RouterStateSnapshot) {
let token = await this.isJwtValid(); //I need get the value of token here
if(token) {
console.log(token) // this is showed first
if (ruta.routeConfig.path == "login") {
this.router.navigate(["/tabs/tab1"]);
}
return true;
}
this.storage.clear();
this.router.navigate(["/login"]);
return false;
}
}
``
我需要获取 returns this.isJwtValid()
的值,但目前它没有返回 promise
的结果值,代码继续其流程而不会停在那里,我需要在这一行中得到这个 promise
的结果:
let token = this.isJwtValid() //I need get the value of the promise in this line
继续我的逻辑。
我该怎么做?
这是我的代码:
export class verificarToken implements CanActivate {
constructor(private router: Router, private storage: Storage) {}
async isJwtValid() {
const jwtToken: any = await this.storage.get("token");
console.log(jwtToken); /// this is showed second
if (jwtToken) {
try {
return JSON.parse(atob(jwtToken.split(".")[1]));
} catch (e) {
return false;
}
}
return false;
}
canActivate(ruta: ActivatedRouteSnapshot, estado: RouterStateSnapshot) {
let token = this.isJwtValid(); //I need get the value of token here
if(token) {
console.log(token) // this is showed first
if (ruta.routeConfig.path == "login") {
this.router.navigate(["/tabs/tab1"]);
}
return true;
}
this.storage.clear();
this.router.navigate(["/login"]);
return false;
}
}
CanActivate 可以 returns 一个 Promise 或 Observable 或 Value, 所以你可以这样做。
canActivate(ruta: ActivatedRouteSnapshot, estado: RouterStateSnapshot) {
return this.isJwtValid().then(token => {
if (token) {
console.log(token) // this is showed first
if (ruta.routeConfig.path == "login") {
this.router.navigate(["/tabs/tab1"]);
return true;
}
this.storage.clear();
this.router.navigate(["/login"]);
return false;
});
}
}
canActivate 也可以 return 承诺。因此利用 async/await.
async canActivate(ruta: ActivatedRouteSnapshot, estado: RouterStateSnapshot) {
let token = await this.isJwtValid(); //I need get the value of token here
if(token) {
console.log(token) // this is showed first
if (ruta.routeConfig.path == "login") {
this.router.navigate(["/tabs/tab1"]);
}
return true;
}
this.storage.clear();
this.router.navigate(["/login"]);
return false;
}
}
``