【问题标题】:How to change only params of route in angular2?如何仅更改 angular2 中的路线参数?
【发布时间】:2017-03-10 01:49:34
【问题描述】:

我有一个方法 add() 在此路由中调用的组件上:'model/:id'。 现在,我想添加一个新模型,为此,需要将路由更改为:'model/0';

add(){
    this.route.navigate(['model','0']);
}

我需要动态地使用“模型”路由来执行此操作,因为 路线可以改变,有可能吗?

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    在你的构造函数中导入ActivatedRoute

    construction(private route: ActivatedRoute, ...) {}
    add() {
      this.router.navigate(['../', '0'], {relativeTo: this.route});
    }
    

    【讨论】:

      【解决方案2】:

      试试这个

      this.route.navigate(['model/0']);
      

      【讨论】:

      • 虽然此代码 sn-p 可以解决问题,包括解释 really helps 以提高您的帖子质量。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
      【解决方案3】:

      你可以这样做

        this.router.navigate(['/model', 0]);
      

      更多信息请参考this

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-16
        • 2019-04-17
        • 1970-01-01
        • 1970-01-01
        • 2019-06-18
        • 1970-01-01
        相关资源
        最近更新 更多