- Angular Router
- Victor Savkin
- 331字
- 2021-07-09 19:20:32
Activating components

At this point, we have a router state. The router can now activate this state by instantiating all the needed components, and placing them into appropriate router outlets.
To understand how it works, let's take a look at how we use router outlets in a component template.
The root component of the application has two outlets: primary and popup.
@Component({ template: ` ... <router-outlet></router-outlet> ... <router-outlet name="popup"></router-outlet> ` }) class MailAppCmp { }
Other components, such as ConversationCmp
, have only one:
@Component({ template: ` ... <router-outlet></router-outlet> ... ` }) class ConversationCmp { }
Other components, such as ConversationCmp
, have only one:
@Component({ template: ` ... <router-outlet></router-outlet> ... ` }) class ConversationCmp { }
Now imagine we are navigating to /inbox/33/messages/44(popup:compose)
.
That's what the router will do. First, it will instantiate ConversationCmp
and place it into the primary outlet of the root component. Then, it will place a new instance of ComposeCmp
into the 'popup'
outlet. Finally, it will instantiate a new instance of MessageCmp
and place it in the primary outlet of the just created conversation component.
Using parameters
Often components rely on parameters or resolved data. For instance, the conversation component probably need to access the conversation object. We can get the parameters and the data by injecting ActivatedRoute
.
@Component({...}) class ConversationCmp { conversation: Observable<Conversation>; id: Observable<string>; constructor(r: ActivatedRoute) { // r.data is an observable this.conversation = r.data.map(d => d.conversation); // r.params is an observable this.id = r.params.map(p => p.id); } }
If we navigate from inbox/33/messages/44(popup:compose)
to /inbox/34/messages/45(popup:compose)
, the data observable will emit a new map
with the new object, and the conversation component will display the information about Conversation 34.
As you can see the router exposes parameters and data as observables, which is convenient most of the time, but not always. Sometimes what we want is a snapshot of the state that we can examine at once.
@Component({...}) class ConversationCmp { conversation: Conversation; constructor(r: ActivatedRoute) { const s: ActivatedRouteSnapshot = r.snapshot; this.conversation = s.data['conversation']; // Conversation } }
- 深入理解Bootstrap
- Learning Cython Programming
- 算法訓練營:入門篇(全彩版)
- ASP.NET 3.5程序設計與項目實踐
- 利用Python進行數據分析(原書第3版)
- C/C++程序員面試指南
- 智能手機故障檢測與維修從入門到精通
- Practical Microservices
- Java7程序設計入門經典
- Learning WordPress REST API
- 絕密原型檔案:看看專業產品經理的原型是什么樣
- JavaScript設計模式與開發實踐
- Visual FoxPro程序設計(第二版)
- 大數據高并發Redis一本通
- Mastering Software Testing with JUnit 5