本文介绍了如何通过控制器动作在ember 2.x中以程序方式添加组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在面对同样的问题在


但是,由于我使用ember cli,我无法这样做。
这是我的源代码

How to programatically add component via controller actionHowever 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'));
        }
    },
});

当我尝试运行此代码时,我会收到未定义的错误。还有组件的名称是author-name。

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?

推荐答案

您需要导入组件,那么您不需要Ember Global。

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

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

另一种方法是一个项目的数组,并从那里创建一个作者NameComponent的列表。

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}}

这篇关于如何通过控制器动作在ember 2.x中以程序方式添加组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 11:44