Angular 2 - 根据需要添加URL参数

时间:2016-03-18 10:45:53

标签: angular angular2-routing

是否有一些如何使用代码添加新的URL参数,例如:

http://example.com/mypage?par=1&par2=2

我需要在代码中使用:

if (myVar == 'abc') { 
    // add a par3=3 without navigate/refresh the page - just to 
    // add a decoration on the URL for bookmarks purposes, 
    // for example: when user bookmark it and go back, 
    //I will be displaying the same dialog box 
}

2 个答案:

答案 0 :(得分:1)

你可以使用Angulars Location。通过这种方式,只有URL被更新,但路由器没有导航到它。

import {Location} from 'angular2/router';
 class Component {
  constructor(location: Location) {
    location.go('/foo');//Add your params here
  }
}

答案 1 :(得分:0)

如果您实施CanReuse并返回true,则可以使用添加的参数重新导航到网址,而不会重新加载所有内容。

相关问题