Angular:如何在不更改路线的情况下更新Queryparams
我正在尝试从组件中更新(添加,删除)Queryparams。在AngularJS中,过去可能是可能的:
$location.search('f', 'filters[]'); // setter
$location.search()['filters[]']; // getter
我有一个带有用户可以过滤,订购等列表的应用URL或与其他人共享。
但是,我不希望每次选择过滤器时重新加载。
这对新路由器可行吗?
I am trying to update (add, remove) queryParams from a component. In angularJS, it used to be possible thanks to :
$location.search('f', 'filters[]'); // setter
$location.search()['filters[]']; // getter
I have an app with a list that the user can filter, order, etc and I would like to set in the queryParams of the url all the filters activated so he can copy/paste the url or share it with someone else.
However, I don't want my page to be reloaded each time a filter is selected.
Is this doable with the new router?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您可以使用新的查询参数导航到当前路由,这不会重新加载您的页面,但会更新查询参数。
类似:
请注意,尽管它不会重新加载页面,但它会将新条目推向浏览器的历史记录。如果要在历史记录中替换它而不是在其中添加新值,则可以使用
{queryparams:queryparams,replaceurl:true}
。编辑:
正如注释中已经指出的那样,
[]
和我的原始示例中缺少relativeto
属性,因此它也可以更改路由,而不仅仅是查询参数。正确的this.router.navigate
在这种情况下将是:将新的参数值设置为
null
将从URL中删除参数。You can navigate to the current route with new query params, which will not reload your page, but will update query params.
Something like :
Note, that whereas it won't reload the page, it will push a new entry to the browser's history. If you want to replace it in the history instead of adding new value there, you could use
{ queryParams: queryParams, replaceUrl: true }
.EDIT:
As already pointed out in the comments,
[]
and therelativeTo
property was missing in my original example, so it could have changed the route as well, not just query params. The properthis.router.navigate
usage will be in this case:Setting the new parameter value to
null
will remove the param from the URL.@radosławRoszkowiak的答案几乎是正确的,除了
relativeto:this.route
是必需的:@Radosław Roszkowiak's answer is almost right except that
relativeTo: this.route
is required as below:在Angular 5中,您可以轻松获取并修改 urltree 通过解析当前URL。这将包括查询参数和片段。
修改查询参数的“正确方法”可能是
createurltree 像下面一样,在下面可以从当前创建一个新的urltree,同时让我们使用< href =“ https://angular.io/api/router/navigationExtras” rel =“ noreferrer”> navigationextras 。
为了通过这种方式删除查询参数,您可以将其设置为
undefined
或null
。In Angular 5 you can easily obtain and modify a copy of the urlTree by parsing the current url. This will include query params and fragments.
The "correct way" to modify a query parameter is probably with the
createUrlTree like below which creates a new UrlTree from the current while letting us modify it using NavigationExtras.
In order to remove a query parameter this way you can set it to
undefined
ornull
.大多数投票的答案部分对我有用。浏览器URL保持不变,但我的
RouterLinkactive
在导航后不再工作。我的解决方案是使用乳液。go:
我使用httpparams来构建查询字符串,因为我已经在使用httpclient将其发送信息。但是您可以自己构建它。
this._router.url.split(“?”)[0]
是从当前URL中删除所有以前的查询字符串。The answer with most vote partially worked for me. The browser url stayed the same but my
routerLinkActive
was not longer working after navigation.My solution was to use lotation.go:
I used HttpParams to build the query string since I was already using it to send information with httpClient. but you can just build it yourself.
and the
this._router.url.split("?")[0]
, is to remove all previous query string from current url.尝试
除了单引号外,还适用于相同的路线导航。
Try
will work for same route navigation other than single quotes.
如果您想更改查询参数而无需更改路由。见下文
示例可能会帮助您:
当前路线是:
/search
&amp;目标路由为(没有重新加载页面):
/search?query = love
请注意:您可以使用
this.router.navigate(['search'']
而不是this.router.navigate(['。']
If you want to change query params without change the route. see below
example might help you:
current route is :
/search
& Target route is(without reload page) :
/search?query=love
please note : you can use
this.router.navigate( ['search']
instead ofthis.router.navigate( ['.']
我最终将
urltree
与location.go
不确定
toString
可能会引起问题,但不幸的是location.go
,似乎是基于字符串的。I ended up combining
urlTree
withlocation.go
Not sure if
toString
can cause problems, but unfortunatelylocation.go
, seems to be string based.更好的是 - 只需html:
&lt; a [routerlink] =“ []” [queryparams] =“ {key:'value'}”&gt; QUERY参数link&lt&lt&a&gt;
请注意空的数组而不是仅仅执行
routerlink =“”
或[routerlink] =“''''''
Better yet - just HTML:
<a [routerLink]="[]" [queryParams]="{key: 'value'}">Your Query Params Link</a>
Note the empty array instead of just doing
routerLink=""
or[routerLink]="''"
与浏览器的URL相互作用而不是路由时,应使用Angular的位置服务。这就是为什么我们要使用位置服务。
Angulars httppparams 用于创建查询参数。请记住,httpparam是不可变的,这意味着在创建值时必须链接它。
最后,使用
this._location.replacestate
更改为URL而无需重新加载页面/路由和本机JSlocation.path.path
以获取无参数的URL,以每次重置参数时间。Angular's Location service should be used when interacting with the browser's URL and not for routing. Thats why we want to use Location service.
Angulars HttpParams are used to create query params. Remember HttpParams are immutable, meaning it has to be chained when creating the values.
At last, using
this._location.replaceState
to change to URL without reloading the page/route and native jslocation.path
to get the url without params to reset the params every time.首先,我们需要从Angular Router导入路由器模块并声明其别名名称
它将在当前IE激活路线中更新以下ie
并将其称为查询参数,
它将在“ ABC”路由中更新查询参数以及三个查询参数
以获取查询参数: -
First, we need to import the router module from angular router and declare its alias name
It will update query params in the current i.e activated route
and name as query params
It will update query params in the "abc" route along with three query paramters
For fetching query params:-
为了维护路径并仅更改参数,您可以使用此代码:
希望它有帮助!
To maintain the path and only change the parameter, you can use this code:
Hope it helps!
我有一个有趣的情况,我们只在所有路线上都使用一个组件。这就是路由的样子:
因此,基本上,路径
/
,/compision>/code>和
/pipeline
都具有与必须为加载。而且,由于Angular会阻止组件的重新加载,如果它们先前已加载到DOM中,则路由器的导航
方法返回了始终以null
解决的承诺。为了避免这种情况,我必须使用
onsameurlnavigation
。通过将此值设置为'reload'
,我设法将路由器 navigate 与更新的查询字符串参数:I've had an interesting situation where we used only one component for all routes we had. This is what routes looked like:
So, basically, paths
/
,/companies
and/pipeline
were all having the same component that had to be loaded. And, since Angular prevents reloading of the components if they were previously loaded in the DOM, Router'snavigate
method returned a Promise that always resolved withnull
.To avoid this, I had to use
onSameUrlNavigation
. By setting this value to'reload'
, I managed to make the router navigate to the same URL with the updated query string parameters:您可以使用[]并在Extras添加参数时导航到同一URL,最后添加repleastUrl:true(替换Angular Doc )以超越最后一个当前的历史。
而且,如果您需要在每个导航上进行操作,则可以使用路由器。事件如下。
you can navigate to the same url using [] and in extras add the params, finally add replaceUrl: true (replaceUrl Angular Doc) to overrride the last current history.
and if you need to do on every navigation you can use router.events as follow.
另外,您可以添加行为主题:
我更改了我的代码:
到:
当您需要刷新您的订阅时,您需要致电以下:
也不要忘记将其添加到Ngondestroy中
Also you can add BehaviorSubject like:
I changed my code from that:
to:
And when you need refresh your subscription, you need call this:
Also don't forget add unsubscribe from that to ngOnDestroy