简体   繁体   English

如何将动态段传递给余烬组件

[英]How to pass dynamic segment to ember component

How do I pass a dynamic segment in my url to a component? 如何将网址中的动态细分传递给组件? I can get the parameters in my model but i have no idea, how to pass that on to the component. 我可以在模型中获取参数,但是我不知道如何将其传递给组件。 What I do right now is: 我现在要做的是:

export default Ember.Route.extend({
    sectionId: 0,

    model(params) {
        this.set('sectionId', params.section_id);
        return this.store.findAll('content');
    }
});

And in the Template: 并在模板中:

{{#component id=sectionId}}{{/component}}

But when I log the id in my component, it says undefined. 但是,当我将ID登录到组件中时,它显示为undefined。

You can set controller properties in setupController hook. 您可以在setupController挂钩中设置控制器属性。

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'));
     }
});

Your controller can have access to the params , so make sure you declare that in your controller. 您的控制器可以访问params ,因此请确保在控制器中声明了这些参数

For example, for your controller: 例如,对于您的控制器:

export default Ember.Controller.extend({
  queryParams: ['section'],
  section: null
});

And in your template: 在您的模板中:

{{#component id=section}}{{/component}}

Note that this is assuming your url looks something like http://example.com/posts/1?section=3 请注意,这是假设您的网址看起来像http://example.com/posts/1?section=3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM