How to change only params of route in angular2?

2019-06-14 11:20发布

I have a method add() on component that is called in this route: 'model/:id'. Now, i want to add a new model, and for do this, need change the route to: 'model/0';

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

I need to do this with the 'model' route dynamically, because the route can change, it's possible ?

3条回答
霸刀☆藐视天下
2楼-- · 2019-06-14 11:56

You can do it by this way

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

For more information refer this

查看更多
在下西门庆
3楼-- · 2019-06-14 12:10

In your constructor import ActivatedRoute

construction(private route: ActivatedRoute, ...) {}
add() {
  this.router.navigate(['../', '0'], {relativeTo: this.route});
}
查看更多
女痞
4楼-- · 2019-06-14 12:15

Try this

this.route.navigate(['model/0']);
查看更多
登录 后发表回答