docs(router): Update description and example for Resolve interface (#31810)

PR Close #31810
This commit is contained in:
Ferdinand Malcher 2019-07-23 23:03:46 +02:00 committed by Miško Hevery
parent 9c153cfb3e
commit b934898e45
1 changed files with 22 additions and 21 deletions

View File

@ -265,23 +265,20 @@ export type CanDeactivateFn<T> =
* @description * @description
* *
* Interface that classes can implement to be a data provider. * 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.
* *
* ``` * ```
* class Backend { * @Injectable({ providedIn: 'root' })
* fetchTeam(id: string) { * export class HeroResolver implements Resolve<Hero> {
* return 'someTeam'; * constructor(private service: HeroService) {}
* }
* }
*
* @Injectable()
* class TeamResolver implements Resolve<Team> {
* constructor(private backend: Backend) {}
* *
* resolve( * resolve(
* route: ActivatedRouteSnapshot, * route: ActivatedRouteSnapshot,
* state: RouterStateSnapshot * state: RouterStateSnapshot
* ): Observable<any>|Promise<any>|any { * ): Observable<any>|Promise<any>|any {
* return this.backend.fetchTeam(route.params.id); * return this.service.getHero(route.paramMap.get('id'));
* } * }
* } * }
* *
@ -289,42 +286,46 @@ export type CanDeactivateFn<T> =
* imports: [ * imports: [
* RouterModule.forRoot([ * RouterModule.forRoot([
* { * {
* path: 'team/:id', * path: 'detail/:id',
* component: TeamComponent, * component: HeroDetailComponent,
* resolve: { * resolve: {
* team: TeamResolver * hero: HeroResolver
* } * }
* } * }
* ]) * ])
* ], * ],
* providers: [TeamResolver] * exports: [RouterModule]
* }) * })
* class AppModule {} * export class AppRoutingModule {}
* ``` * ```
* *
* You can alternatively provide a function with the `resolve` signature: * You can alternatively provide a function with the `resolve` signature:
* *
* ``` * ```
* export const myHero: Hero = {
* // ...
* }
*
* @NgModule({ * @NgModule({
* imports: [ * imports: [
* RouterModule.forRoot([ * RouterModule.forRoot([
* { * {
* path: 'team/:id', * path: 'detail/:id',
* component: TeamComponent, * component: HeroComponent,
* resolve: { * resolve: {
* team: 'teamResolver' * hero: 'heroResolver'
* } * }
* } * }
* ]) * ])
* ], * ],
* providers: [ * providers: [
* { * {
* provide: 'teamResolver', * provide: 'heroResolver',
* useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => 'team' * useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => myHero
* } * }
* ] * ]
* }) * })
* class AppModule {} * export class AppModule {}
* ``` * ```
* *
* @publicApi * @publicApi