如何在Canactivate中调用异步方法|离子,角
我想获得一个令牌来验证用户。我从'@ionic/storage-angular'; 中保存了import {storege}的数据。问题是
storege
方法仅在async
中起作用。模式。
这是存储服务
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage-angular';
@Injectable({
providedIn: 'root'
})
export class StorageService {
private _storage: Storage | null = null;
constructor(private storage: Storage) {
}
public async init() {
const storage = await this.storage.create();
this._storage = storage;
}
public async set(key: string, value: any): Promise<void> {
const data = await this._storage?.set(key, value);
}
public async get(key: string): Promise<any> {
return data await this._storage?.get(key);
}
public async remove(key: string): Promise<void> {
await this._storage?.remove(key);
}
我初始化数据库,调用方法init()
in ngoninit AppComponent
这是guard> guard
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { StorageService } from '../services/storage.service';
@Injectable({
providedIn: 'root'
})
export class UserGuard implements CanActivate {
constructor(
private router: Router,
private storage: StorageService
) {}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean | UrlTree | Observable<boolean | UrlTree> {
const data = this.storage.get('token');
if (!data) {
this.router.navigateByUrl('/login');
}
else {
return true;
}
}
}
I want to get a token to authenticate the users. I save the data with import { Storage } from '@ionic/storage-angular';
the problem is that Storage
methods just works in async
mode.
This is the storage service
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage-angular';
@Injectable({
providedIn: 'root'
})
export class StorageService {
private _storage: Storage | null = null;
constructor(private storage: Storage) {
}
public async init() {
const storage = await this.storage.create();
this._storage = storage;
}
public async set(key: string, value: any): Promise<void> {
const data = await this._storage?.set(key, value);
}
public async get(key: string): Promise<any> {
return data await this._storage?.get(key);
}
public async remove(key: string): Promise<void> {
await this._storage?.remove(key);
}
I initialize the database calling the method init()
in ngOnInit AppComponent
This is the Guard
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { StorageService } from '../services/storage.service';
@Injectable({
providedIn: 'root'
})
export class UserGuard implements CanActivate {
constructor(
private router: Router,
private storage: StorageService
) {}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean | UrlTree | Observable<boolean | UrlTree> {
const data = this.storage.get('token');
if (!data) {
this.router.navigateByUrl('/login');
}
else {
return true;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样更改您的功能:
Change your function like so: