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

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.

主站蜘蛛池模板: 山阳县| 汉阴县| 武鸣县| 德江县| 新巴尔虎左旗| 景宁| 永嘉县| 柘城县| 沾化县| 偃师市| 阿城市| 新昌县| 吉水县| 京山县| 竹山县| 潮州市| 湛江市| 铁力市| 闵行区| 临夏市| 东莞市| 承德县| 赤水市| 浮梁县| 宁波市| 渝北区| 张家港市| 深水埗区| 永新县| 江北区| 拉萨市| 四平市| 吉首市| 昭苏县| 义马市| 柯坪县| 泗阳县| 德庆县| 高台县| 无锡市| 西贡区|