- 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.
推薦閱讀
- JavaScript全程指南
- Mastering SVG
- HTML5 移動Web開發從入門到精通(微課精編版)
- Visual C++數字圖像模式識別技術詳解
- Python貝葉斯分析(第2版)
- Corona SDK Mobile Game Development:Beginner's Guide(Second Edition)
- 零基礎學C語言第2版
- Principles of Strategic Data Science
- Python入門很輕松(微課超值版)
- Django Design Patterns and Best Practices
- Building Business Websites with Squarespace 7(Second Edition)
- 百萬在線:大型游戲服務端開發
- Swift High Performance
- Java多線程并發體系實戰(微課視頻版)
- ASP.NET Core and Angular 2