- Angular Router
- Victor Savkin
- 178字
- 2021-07-09 19:20:31
Resolving data
After the router has run the guards, it will resolve the data. To see how it works, let's tweak our configuration from above:
[ { path: ':folder', children: [ { path: '', component: ConversationsCmp, resolve: { conversations: ConversationsResolver } } ] } ]
Where ConversationsResolver
is defined as follows:
@Injectable() class ConversationsResolver implements Resolve<any> { constructor(private repo: ConversationsRepo, private currentUser: User) {} resolve(route: ActivatedRouteSnapshot, state: RouteStateSnapshot): Promise<Conversation[]> { return this.repo.fetchAll(route.params['folder'], this.currentUser); } }
Finally, we need to register ConversationsResolver
when bootstrapping our application:
@NgModule({ //... providers: [ConversationsResolver], bootstrap: [MailAppCmp] }) class MailModule { } platformBrowserDynamic().bootstrapModule(MailModule);
Now when navigating to /inbox
, the router will create a router state, with an activated route for the conversations component. That route will have the folder
parameter set to inbox
. Using this parameter with the current user, we can fetch all the inbox conversations for that user.
We can access the resolved data by injecting the activated route object into the conversations component:
@Component({ template: ` <conversation *ngFor="let c of conversations | async"></conversation> ` }) class ConversationsCmp { conversations: Observable<Conversation[]>; constructor(route: ActivatedRoute) { this.conversations = route.data.pluck('conversations'); } }
推薦閱讀
- HTML5+CSS3+JavaScript從入門到精通:上冊(微課精編版·第2版)
- 數字媒體應用教程
- Mobile Web Performance Optimization
- Testing with JUnit
- ASP.NET Core Essentials
- 數據庫系統原理及MySQL應用教程
- C#程序設計
- Advanced Oracle PL/SQL Developer's Guide(Second Edition)
- Clojure Reactive Programming
- 石墨烯改性塑料
- XML程序設計(第二版)
- C/C++代碼調試的藝術(第2版)
- 面向對象分析與設計(第3版)
- Visual Basic 開發從入門到精通
- 算法(第4版)