Resolve
一个接口,某些类可以实现它以扮演一个数据提供者。
Interface that classes can implement to be a data provider. A data provider class can be used with the router to resolve data during navigation. The interface defines a resolve()
method that will be invoked when the navigation starts. The router will then wait for the data to be resolved before the route is finally activated.
interface Resolve<T> {
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<T> | Promise<T> | T
}
说明
@Injectable({ providedIn: 'root' })
export class HeroResolver implements Resolve<Hero> {
constructor(private service: HeroService) {}
resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<any>|Promise<any>|any {
return this.service.getHero(route.paramMap.get('id'));
}
}
@NgModule({
imports: [
RouterModule.forRoot([
{
path: 'detail/:id',
component: HeroDetailComponent,
resolve: {
hero: HeroResolver
}
}
])
],
exports: [RouterModule]
})
export class AppRoutingModule {}
你还可以转而提供一个具有 resolve
签名的函数:
You can alternatively provide a function with the resolve
signature:
export const myHero: Hero = {
// ...
}
@NgModule({
imports: [
RouterModule.forRoot([
{
path: 'detail/:id',
component: HeroComponent,
resolve: {
hero: 'heroResolver'
}
}
])
],
providers: [
{
provide: 'heroResolver',
useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => myHero
}
]
})
export class AppModule {}
方法
参数
返回值
|