[英]How to access the params in controller ember.js
這是我的 router.js 代碼。
this.route('contact',{'path': '/contact/:chat_id'});
這是我的 route.js 代碼。
model(params) {
return this.store.findRecord("chat", params.chat_id)
},
這是我的 controller.js 代碼,我可以這樣使用嗎? 它將錯誤顯示為空值,請幫助我。 如何在控制器中使用參數
recordChat: function() {
var chat = this.get(params.chat_id)
Ember.RSVP.hash({
offer_id: chat,
})
}
為 2021 年編輯:現在在 Ember 中實際上可能有一種更簡單的方法來做到這一點。 見https://guides.emberjs.com/v3.27.0/routing/query-params/
原答案
我認為最簡單的答案是在路由中創建一個變量,然后在 setupController 中設置它:
your_route.js
export default Ember.Route.extend({
model(params) {
this.set('myParam',params.my_Params);
// Do Model Stuff...
},
setupController(controller, model) {
// Call _super for default behavior
this._super(controller, model);
// Implement your custom setup after
controller.set('myParam', this.get('myParam'));
}
});
在你的 route.js 中,你需要像這樣調用 setupController 函數:
setupController(controller, model) {
this._super(...arguments);
controller.set('chat', model);
}
現在您可以通過調用在控制器中訪問它:
recordChat() {
const chat = this.get('chat');
// if you don't call the setupController function, you could also do:
// const chat = this.get('model') or this.get('model.id') if you want the id only
}
更新:
在這里看到工作twiddle
我認為您可以從模型中獲取所需的 id,因為您使用參數 chat_id 來查找記錄,並且該 id 現在是您的聊天對象(即路由模型本身)的一部分。
因此,在您的控制器中,您只需要執行以下操作: this.get('model').id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.