是的,你体内的一切
<template>
是在
this
范围,所以
这
是未定义的。
v-model
只是对
:value
和
@input
,因此可以使用自定义事件和
价值
.
还可以将计算属性与getter和setter一起使用;类似于
computed: {
model: {
get: function () {
return this.$store.state[this.$route.params.name]
},
set: function (value) {
this.$store.commit('updateValue', { name: this.$route.params.name, value})
}
}
}
编辑
如果您在setter中有更多的逻辑要做,我会像这样分开它,保持getter简单,并坚持使用一个计算属性;
computed: {
model: {
get: function () {
return this.$store.state[this.$route.params.name]
},
set: function (value) {
switch(this.$route.params.name) {
case 'foo':
return this.foo(value)
default:
return this.bar(value)
}
}
}
},
methods: {
foo(val) {
this.$store.commit(...)
},
bar(val) {
this.$store.commit(...)
}
}