Angular 2更新URL路径参数没有重定向

时间:2018-02-14 12:35:02

标签: angular angular2-routing query-parameters

使用Angular 2如何在不重定向页面的情况下更新URL路径。

我知道您可以使用ActivatedRoute queryParamMap Observable访问参数,但是如果我想更改这些值并将其显示在URL中该怎么办。

更新URL中的值可以轻松实现书签功能。

我不想重定向,因为我添加的查询参数是我想存储的简单数据过滤器。

2 个答案:

答案 0 :(得分:1)

在组件构造函数中,将查询参数订阅到模型中。

public static implicit operator bool(InterlockedBool obj)
{
    return obj.Value;
}

现在,将监听器功能注册到视图模型,或者将监视添加到模型中。然后有一个导航功能,您可以在模型更新后调用:

this.route.queryParams.subscribe(params => {
  this.myValue = params['myValue'];
});

答案 1 :(得分:0)

只有两个不同路线的相同组件

const appRoutes: Routes = [
    { path: 'somepath', component: someComponent }, // yourapp/somepath
    { path: 'anotherpath', component: someComponent }, // yourapp/anotherpath
];
<a class="nav-link" routerLink="/anotherpath" routerLinkActive="active"> change path</a>
相关问题