- 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'); } }
推薦閱讀
- Unity 2020 Mobile Game Development
- C/C++算法從菜鳥到達人
- Java 9 Programming Blueprints
- Python高級編程
- Java面向對象程序開發及實戰
- 從0到1:Python數據分析
- C語言程序設計上機指導與習題解答(第2版)
- 基于SpringBoot實現:Java分布式中間件開發入門與實戰
- Mastering C++ Multithreading
- Java EE企業級應用開發教程(Spring+Spring MVC+MyBatis)
- Test-Driven JavaScript Development
- Python Machine Learning Blueprints:Intuitive data projects you can relate to
- Oracle Database XE 11gR2 Jump Start Guide
- 大話代碼架構:項目實戰版
- Processing開發實戰