Resolve

Interface that class can implement to be a data provider.

一个接口,某些类可以实现它以扮演一个数据提供者。

interface Resolve<T> { resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<T> | Promise<T> | T }

说明

class Backend { fetchTeam(id: string) { return 'someTeam'; } } @Injectable() class TeamResolver implements Resolve<Team> { constructor(private backend: Backend) {} resolve( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable<any>|Promise<any>|any { return this.backend.fetchTeam(route.params.id); } } @NgModule({ imports: [ RouterModule.forRoot([ { path: 'team/:id', component: TeamCmp, resolve: { team: TeamResolver } } ]) ], providers: [TeamResolver] }) class AppModule {}

You can alternatively provide a function with the resolve signature:

你还可以转而提供一个具有 resolve 签名的函数:

@NgModule({ imports: [ RouterModule.forRoot([ { path: 'team/:id', component: TeamCmp, resolve: { team: 'teamResolver' } } ]) ], providers: [ { provide: 'teamResolver', useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => 'team' } ] }) class AppModule {}

方法

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<T> | Promise<T> | T

参数

route

Type: ActivatedRouteSnapshot.

state

Type: RouterStateSnapshot.

返回值

Observable<T> | Promise<T> | T