How to programatically add component via controller action in ember 2.x

主宰稳场 提交于 2020-01-05 00:02:37

问题


I am being faced with the same problem on

How to programatically add component via controller action However since I am using ember cli, I am unable to do so. Here is my source code

import Ember from "ember";
export default Ember.Component.extend({
    actions : {
        remove : function(){
            this.remove();
        },
        add : function()
        {
            Ember.AuthorNameComponent.create().appendTo($('#authors'));
        }
    },
});

When I try to run this code, I get undefined error. Also name of component is author-name.

Any help, how can I create component via programmatically?


回答1:


You need to import the component, then you don't need the Ember Global.

import AuthorNameComponent from '../components/author-name-component'

Another way is to have an array of items and base the list of AuthorNameComponent from that.

{{#each items as |item|}}
    {{author-name name=item.name}}
{{/each}}


来源:https://stackoverflow.com/questions/32172677/how-to-programatically-add-component-via-controller-action-in-ember-2-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!