我在解决问题时遇到了这种情况。考虑以下场景,提供 jsfiddle 链接:
http://jsfiddle.net/fp5Lt7zx/1/
enyo.kind({
name:'base',
components:[
{name:'button', kind:'moon.Button'}
],
create: function(){
this.inherited(arguments);
this.$.button.createComponent({
name:'tag',
classes:'list-recording-tag',
components: [{
content: "NEW",
classes: "list-recording-tag-font"
}]
});
}
});
new base().renderInto(document.body);
这工作正常,但是当我尝试在 rendered 函数中给出它而不是编写 create 中给出的功能时,问题就来了。以下是具有相同功能的渲染函数的链接。
使用渲染函数,动态创建的“标签”组件将不会渲染。所以为了强制渲染,我必须添加这行代码
this.$.button.$.tag.render(); //this way is not recommended though
为什么需要在rendered函数中强制渲染标签组件,而不是在create函数中。除此之外,他们之间还有哪些差异需要考虑?
最佳答案
如果将控件添加到已渲染的控件(就像将其放入渲染函数中时所做的那样),则必须强制浏览器重绘,因此需要在新控件上调用 .render() 。如果将其保留在 create 中,则组件会在呈现其包含的控件之前构造,因此当它存在时就会存在。
关于javascript - Enyo 中创建函数和渲染函数之间的场景差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27809683/