Computed property "xxx" was assigned to but it has no setter.
vue使用computed属性计算某些值时,在页面上重新赋值时,报错Computed property "xxxx" was assigned to but it has no setter,翻译成中文就是“计算属性“xxxx”已经分配了,但它没有setter”,理解一下,就是属性xxxx获取了值,但是没有设置值的方法,报错代码示例贴一下:
computed: {
cityOptions() {
return this.$store.state.common.cityOptions
}
},
created() {
this.getCityOptions()
},
methods: {
getCityOptions() {
getCitys().then(res => {
this.cityOptions = res.data
})
}
}
此时我们的代码需要修改为:
computed: {
cityOptions: {
get() {
return this.$store.state.common.cityOptions
},
set(val) {
return val
}
}
},
created() {
this.getCityOptions()
},
methods: {
getCityOptions() {
getCitys().then(res => {
this.cityOptions = res.data
})
}
}