Vue.js 观察一个值,计算另一个值,当观察到变化时如何更新计算值
我在观察一个值,然后计算另一个值 大概是这样的:Vue.js 观察一个值,计算另一个值,当观察到变化时如何更新计算值,vue.js,vuejs2,Vue.js,Vuejs2,我在观察一个值,然后计算另一个值 大概是这样的: computed: { modeText: function () { if(this.mode == 'create') return 'Create' else return 'Edit' } }, watch: { mode(val, old) {
computed: {
modeText: function () {
if(this.mode == 'create')
return 'Create'
else
return 'Edit'
}
},
watch: {
mode(val, old) {
if(val == 'create')
update modeText
else
update modeText
},
},
当监视的值更新时,如何重新计算计算的值?似乎modeText仅依赖于模式,因此您可以选择更简单的方法,而不是使用computed:
data: {
modeText: '',
mode: ''
},
watch: {
mode(val, old) {
this.modeText = this.updateModeText(val);
},
},
methods: {
updateModeText(val) {
if (val === 'create') {
return 'Create';
}
return 'Edit';
}
}
它是反应性的。此模式更改时,modeText应自动更新。