如何更新计算属性?
问题描述:
我是新来的emberjs。 在模板中,我有 {{LabelText的}}如何更新计算属性?
现在,我想 默认情况下,LabelText的应该是, '你好' 如果条件A,然后LabelText的=哇 如果条件B,然后LabelText的=感谢 有没有办法在emberjs中的组件中编写计算属性,这将执行以上要求?
答
在您的组件,
labeltext:Ember.computed('conditionVar',function(){
var val = this.get('conditionVar');
if(Ember.isEqual(val,'conditionA')){
return 'Wow';
}
else if(Ember.isEqual(val,'conditionB')) {
return 'Thanks';
}
return 'Hello';
})
你可能有getter和setter的计算性能,
labeltext: Ember.computed('conditionVar', function() {
get(key){
var val = this.get('conditionVar');
if (Ember.isEqual(val, 'conditionA')) {
return 'Wow';
}else if (Ember.isEqual(val, 'conditionB')) {
return 'Thanks';
}
return 'Hello';
},
set(key, value){
// you can set conditionVar so that it will return the same value when you request it again.
if(value === 'Wow') {
this.set('conditionVar', 'conditionA');
}
else if(value === 'Thanks') {
this.set('conditionVar', 'conditionB');
}
return value;
}
}),
参见官方指南:https://guides.emberjs.com/v2.3.0/object-model/computed-properties/