我有以下解决方案:
export class AbcResolve implements Resolve<any> {
constructor(private abcService: AbcService) { }
resolve(route: ActivatedRouteSnapshot) {
// Also tried returning return this.abcService.data$ here, but did not work
return Observable.create((observer: any) => {
this.abcService.data$.subscribe(data => {
if (data && Object.keys(data).length > 0) {
observer.next(data);
observer.complete();
}
});
});
}
}
我的警卫如下:
@Injectable()
export class AbcGuard implements CanActivate, CanActivateChild {
constructor(private readonly router: Router) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
const expectedToken = route.data["token"] as string;
const tokens = route.data["tokens"] as string[];
return tokens[expectedToken] === true;
}
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.canActivate(route, state);
}
}
我的路线如下:
export const routes: Routes = [
{
path: 'Abc',
component: AbcComponent,
canActivate: [AuthenticationGuard, AbcGuard],
canActivateChild: [AuthenticationGuard, AbcGuard],
data: { expectedToken: "SomeToken" },
resolve: { tokens: AbcResolve }
},
]
由于某种原因,我的Resolve从未被调用。有人可以帮忙吗?
我也尝试过直接从订阅服务返回可观察者,如上面的评论中所述,但这没有用。