- 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'); } }
推薦閱讀
- Progressive Web Apps with React
- JavaScript語言精髓與編程實踐(第3版)
- Developing Mobile Web ArcGIS Applications
- 深入淺出Java虛擬機:JVM原理與實戰(zhàn)
- Visual C++數字圖像模式識別技術詳解
- Java從入門到精通(第4版)
- Functional Programming in JavaScript
- Python Network Programming Cookbook(Second Edition)
- SSM輕量級框架應用實戰(zhàn)
- Java網絡編程核心技術詳解(視頻微課版)
- RESTful Java Web Services(Second Edition)
- Swift語言實戰(zhàn)晉級
- IBM Cognos TM1 Developer's Certification guide
- Instant jQuery Boilerplate for Plugins
- C++ System Programming Cookbook