Ember组件是什么布局以及如何编写功能? [英] Ember component what is layout and how to write the functions?

查看:45
本文介绍了Ember组件是什么布局以及如何编写功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个组件,它的输出为:

I created a component, it gives the output as :

import Ember from 'ember';
import layout from '../templates/components/sample-work';

export default Ember.Component.extend({
  layout
});

当我尝试添加一些初始化方法时,例如:

When i try to add some init method like :

import Ember from 'ember';
import layout from '../templates/components/sample-work';

export default Ember.Component.extend({
  layout,
  init(){
    alert.log('hi');
  }
});

我的组件根本没有被调用。这是什么问题?在这里处理组件的正确方法是什么?

My component not at all called. what is the issue here? what is the correct way to handle the component here?

推荐答案

通常,您不需要 layout js 文件中;因为 ember 是一个自以为是的框架,通常会将组件的 js hbs 文件放置在默认情况下可以自动匹配的位置: js 放在 components 和<$下c $ c> hbs 文件放在 templates\components 下。

Normally you do not need the layout within js file; because ember is an opinionated framework and it usually puts the component's js and hbs files to places where it can match them automatically by default: js is placed under components and hbs file is placed under templates\components.

在案件;您将模板文件放置在导入布局所需的 js 文件不直接可用的位置。看看简单我已经为你准备好了。在这个旋转中; my-component2 的模板文件需要作为相应的 js <中的 layout 字段导入。 / code>文件。

In case; you put the template file to a place where it is not directly available to js file you need to import the layout. Take a look at the simple twiddle I have prepared for you. In this twiddle; my-component2's template file needs to be imported as layout field within corresponding js file.

这篇关于Ember组件是什么布局以及如何编写功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆