Angular 2 - 按需添加URL参数

Angular 2 - add URL parameter on demand

是否有一些方法可以使用代码添加新的 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 
}

如果您实施 CanReuse 和 return true,您可以使用添加的参数导航到 URL,而无需重新加载所有内容。

您可以使用 Angulars Location。这样只有 url 得到更新,但路由器不会导航到它。

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