- Angular Router
- Victor Savkin
- 162字
- 2021-07-09 19:20:32
Navigation

So at this point the router has created a router state and instantiated the components. Next, we need to be able to navigate from this router state to another one. There are two ways to accomplish this: imperatively, by calling router.navigate
, or declaratively, by using the RouterLink directive.
Imperative navigation
To navigate imperatively, inject the Router service and call navigate
:
@Component({...}) class MessageCmp { public id: string; constructor(private route: ActivatedRoute, private router: Router) { route.params.subscribe(_ => this.id = _.id); } openPopup(e) { this.router.navigate([{outlets: {popup: ['message', this.id]}}]).then(_ => { // navigation is done }); } }
RouterLink
Another way to navigate around is by using the RouterLink directive:
@Component({ template: ` <a [routerLink]="['/', {outlets: {popup: ['message', this.id]}}]">Edit</a> ` }) class MessageCmp { public id: string; constructor(private route: ActivatedRoute) { route.params.subscribe(_ => this.id = _.id); } }
This directive will also update the href
attribute when applied to an <a>
link element, so it is SEO friendly and the right-click open-in-new-browser-tab behavior we expect from regular links will work.
推薦閱讀
- INSTANT CakePHP Starter
- HTML5游戲開發案例教程
- ASP.NET 3.5程序設計與項目實踐
- Spring快速入門
- Mastering Business Intelligence with MicroStrategy
- Julia for Data Science
- Visualforce Developer’s guide
- Spring技術內幕:深入解析Spring架構與設計原理(第2版)
- Building Dynamics CRM 2015 Dashboards with Power BI
- C編程技巧:117個問題解決方案示例
- 多媒體技術及應用
- SSH框架企業級應用實戰
- 你真的會寫代碼嗎
- 菜鳥成長之路
- Mathematica Data Visualization