- 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.
推薦閱讀
- C++案例趣學
- Developing Middleware in Java EE 8
- Scratch 3.0少兒編程與邏輯思維訓練
- C語言程序設計
- 青少年Python編程入門
- SharePoint Development with the SharePoint Framework
- Visual Basic程序設計
- 深入剖析Java虛擬機:源碼剖析與實例詳解(基礎卷)
- C語言程序設計與應用(第2版)
- 數據結構:Python語言描述
- 你好!Java
- Hands-On ROS for Robotics Programming
- Python面向對象編程(第4版)
- 移動智能系統測試原理與實踐
- 面向對象程序設計及C++實驗指導(第3版)