我坚持在组件 js 中递增和递减一个值。我在我的模板中得到了这个:
<div class="circleBase" {{action 'upHeat' device.status}}> <p> {{heat}} C </p> </div>
现在我只想在单击 div 时增加热量值。我的组件如下所示:
init() {
console.log('init');
this._super(...arguments);
this.errors = [];
},
didRender() {
this.setProperties({
heat: 0
});
},
actions: {
upHeat: function(a){
this.set('heat', this.heat +1)
//or
this.setProperties({
heat: this.heat +1
});
console.log(heat);
}
}
这是行不通的。每次我点击我的 div 时,热值都会增加但不会保存。我的模板仍然显示 0 作为值。
最佳答案
您已重置 didRender()
中的值。
因此,钩子(Hook)将触发渲染,将值重置为 0
。
在 didRender
中使用 console.log(something)
可以看到每当 div
时调用 didRender
Hook > 被点击。
我已经根据您的要求创建了一个小游戏。请看一下。
关于javascript - ember 组件递增/递减值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47953249/