官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 宿松县| 黔江区| 姜堰市| 磴口县| 攀枝花市| 昂仁县| 湟源县| 贵州省| 婺源县| 龙井市| 东明县| 根河市| 阿合奇县| 靖远县| 泉州市| 靖远县| 抚州市| 延川县| 高邮市| 舟曲县| 内黄县| 巴马| 丹江口市| 盈江县| 连云港市| 贵阳市| 金川县| 衢州市| 荆州市| 繁峙县| 闽侯县| 铜鼓县| 太康县| 广南县| 民丰县| 湖北省| 尉犁县| 聂荣县| 招远市| 遂平县| 崇明县|