【发布时间】:2018-06-13 13:50:21
【问题描述】:
我似乎对 Ember 组件的 init 属性做错了。
我的代码看起来像这样。
import Ember from 'ember';
export default Ember.Component.extend({
init(){
this._super(...arguments);
this.set('initial_value', this.get('value'));
},
change(e){
console.log('value should be reset to ' + this.get('initial_value')) //This prints the initial value correctly.
this.set('value', this.get('initial_value')); // this line does not update value.
//this.set('value', 'hello'); //this line does update value.
}
});
为什么我可以用字符串文字更新value,但不能用this.get('initial_value')?
我尝试了这段代码,将init 替换为组件生命周期中的每个函数。我这样做是因为我认为它与渲染有关;还是有的。
这里是twiddle。
【问题讨论】:
-
我觉得这个问题有点混乱:
dup代码是怎么回事……这有关系吗?谁在打电话给change?我尝试使change成为一个动作并添加一个按钮来调用它,并使用{{input...帮助器对value进行正确的双向绑定...但随后取消注释hello行也没有效果。 -
我想我没有更新 twiddle。我需要更改名称并更改一些我正在使用的实际代码来创建旋转。希望它应该被更新。基本上我需要将
change函数中的变量设置为init中设置的变量的值。 -
仍然无法重现问题。如果我将
this.set('value','something else');添加到 init 方法(否则整个旋转的值是'initial value'),然后输入,输入然后离开输入字段导致它按预期重置为'initial value'(或 @ 987654338@ 如果相应的行未注释)。
标签: ember.js render init ember-components