我的路由器是这样的
const routes: Routes = [{
path: '',
canActivate: [AuthGuard],
resolve: {
user: UserResolver,
},
children: [{
path: 'home',
canActivate: [HomeGuard],
component: HomeComponent,
}, {
...otherRoutes,
}],
}];
为了授权访问,HomeGuard
需要一个数据,该数据位于由UserResolver
. 问题是HomeGuard
不等待UserResolver
完成。
@Injectable({
providedIn: 'root',
})
export class UserResolver implements Resolve<any> {
constructor(private api: ApiService) { }
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<User> {
return this.api.getUser().pipe(
delay(1000), // simulates the api call duration
map((user: User) => {
console.log(user);
return user;
}),
);
}
}
在其他 stackoverflow 帖子中,人们声称解析器在导航之前解析对象,但这是否意味着守卫等待父解析器完成?
编辑。可以使用警卫作为解析器吗?