使用父路径的查询参数刷新子路径中的模型 [英] Use query params of parent route to refresh the model in subroute
问题描述
我有一条路线 main
,其中有一个查询参数 search
。
我有子路由 main.sub1
, main.sub2
I have a route main
which has a query param search
.
I have subroutes main.sub1
, main.sub2
转到 / main / sub1?search = hello
或 / main / sub2?search = hello
将 main
路由的查询参数设置为 hello
。这部分工作正常。
Going to /main/sub1?search=hello
or /main/sub2?search=hello
sets the query param of the main
route to hello
. This part works fine.
现在,我希望能够刷新 main.sub1
的模型查询参数 search
在路由 main
中进行了更改,所以我在这里是路由 main的代码.sub1
:
Now I would like to be able to refresh the model of main.sub1
when the query param search
in route main
changes, so I here is the code of the route main.sub1
:
export default Ember.Route.extend({
queryParams: {
search: {
refreshModel: true
}
},
model(params) {
// here I can use params.search to fetch the model for this route
}
}
我认为因为在 main.sub1
的控制器中没有 search
查询参数,Ember会很聪明地猜测它必须使用 main
中的一个。但是现在,当我转到 / main / sub1
时,出现以下错误消息:
I thought that since there is no search
query param in the controller of main.sub1
Ember would be smart enough to guess it has to use the one from main
. But now, when I go to /main/sub1
I have this error message:
断言失败:不允许将多个控制器属性映射到同一查询参数键,但是两个
main:searc h
和main.sub1:search
映射到search
。您可以通过as
config选项将控制器属性之一映射到其他查询参数键来解决此问题,例如search:{as:'other-search'}
Assertion Failed: You're not allowed to have more than one controller property map to the same query param key, but both
main:search
andmain.sub1:search
map tosearch
. You can fix this by mapping one of the controller properties to a different query param key via theas
config option, e.g.search: { as: 'other-search' }
我的猜测是该Ember自动在 main.sub1
中创建一个查询参数 search
,该查询参数与中的查询参数冲突main
,甚至不尝试使用 main
中的那个。
My guess is that Ember automatically creates a query param search
in main.sub1
which conflicts with the one from main
, and does not even try to use the one from main
.
如何我可以克服这个问题吗?
How could I overcome this problem?
简单地说:如何使用父路由的属性作为子路由的查询参数?
Simply put: how can I use an attribute of a parent route as a query param of a subroute?
谢谢!
推荐答案
我遇到了同样的问题。
使用主路由的queryParams完全足够。
您无需在子路由中添加任何queryParams。
refreshModel设置属于主路由,所有子路由也会刷新。
It is completely enough to use the queryParams of the "main" route. You don't need to add any queryParams to the sub-routes. The refreshModel setting belongs to the main-route and all child routes will refresh as well.
也可以将其用于子路由模型:
this.paramsFor('main')
Also you can use for the sub-routes model:
this.paramsFor(‘main’)
请参阅: https:// describe.emberjs.com/t/refresh-model-when-the-queryparam-of-a-parent-route-changes/14903/2
这篇关于使用父路径的查询参数刷新子路径中的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!