[英]Arbitrary query params on Ember.js
在Ember.js, query-params
可以被用來link-to
生成的鏈接,其中包括一個查詢字符串。 例如:
<p>{{link-to "Search" 'search' (query-params q='london')}}</p>
目前尚不明顯的是如何將任意參數傳遞給query-params
。 這是:傳遞一個對象,其鍵/值將構成查詢字符串。 例如以下內容(將引發錯誤):
// At the component
myParams: {
q: 'london',
},
{{!-- At the template --}}
<p>{{link-to "Search" 'search' (query-params myParams)}}</p>
<!-- The end result -->
<p><a href="/search?q=london">Search</a></p>
查看Ember的源代碼,我可以看到如何做到這一點。 我可以將一個看起來像query-params
返回的對象放在一起。 確實如此,以下對象將通過link-to
解釋為我想要的方式:
// At the component
myParams: {
isQueryParams: true,
values: {
q: 'london',
},
},
{{!-- At the template --}}
<p>{{link-to "Search" 'search' myParams}}</p>
但是,對我來說,這似乎是一個內部專用接口。 所以我的問題是:是否有批准的方法可以做到這一點? (還有,這是什么?)
目前唯一的解決方案是創建自己的幫助器:
// app/helpers/hash-query-params.js
import Ember from 'ember';
export function hashQueryParams([hashParams]) {
return Ember.Object.create({
isQueryParams: true,
values: hashParams
});
}
export default Ember.Helper.helper(hashQueryParams);
然后像
<p>{{link-to "Search" 'search' (hash-query-params myParams)}}</p>
這可以由Ember支持。 使用此幫助程序打開RFC或創建一個插件! :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.