Ember component what is layout and how to write the functions?

末鹿安然 提交于 2019-12-25 01:35:29

问题


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?


回答1:


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.

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.




回答2:


You should always call this._super(...arguments) inside your init function



来源:https://stackoverflow.com/questions/47882534/ember-component-what-is-layout-and-how-to-write-the-functions

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