Javascript 如何将动态段传递给余烬组件
如何将url中的动态段传递给组件?我可以在我的模型中获得参数,但我不知道如何将其传递给组件。我现在做的是:Javascript 如何将动态段传递给余烬组件,javascript,ember.js,Javascript,Ember.js,如何将url中的动态段传递给组件?我可以在我的模型中获得参数,但我不知道如何将其传递给组件。我现在做的是: export default Ember.Route.extend({ sectionId: 0, model(params) { this.set('sectionId', params.section_id); return this.store.findAll('content'); } }); 在模板中: {{#compo
export default Ember.Route.extend({
sectionId: 0,
model(params) {
this.set('sectionId', params.section_id);
return this.store.findAll('content');
}
});
在模板中:
{{#component id=sectionId}}{{/component}}
但是当我在我的组件中记录id时,它表示未定义 ,因此请确保在控制器中声明
例如,对于控制器:
export default Ember.Controller.extend({
queryParams: ['section'],
section: null
});
在模板中:
{{#component id=section}}{{/component}}
注意,这是假设您的url看起来像
http://example.com/posts/1?section=3
您可以在setupController
hook中设置控制器属性
export default Ember.Route.extend({
sectionId: 0,
model(params) {
this.set('sectionId', params.section_id);
return this.store.findAll('content');
},
setupController(controller, model) {
controller.set('sectionId', this.get('sectionId'));
}
});
模板中的
sectionId
在route.Ember.set(this.get('controller')、'sectionId',params.section_id)上找不到属性。然而,我不明白使用分段路线和不在模型上查询任何内容的意义。也许你可以发布更多的代码。