1 回答
TA贡献1827条经验 获得超4个赞
看起来没问题,<div v-if="col.code">但是将 select 绑定到计算属性是错误的,因此您应该使用计算 setter:
computed: {
col: {
get(){
return this.$store.state.col;
},
set(newVal){
this.$store.commit('UPDATE_COL',newVal);
}
}
}
Vue.use(Vuex)
//store instance
const store = new Vuex.Store({
state: {
col: {
code: 'A'
}
},
mutations: {
UPDATE_COL(state, payload) {
state.col = payload
}
}
})
//vue instance
let app = new Vue({
el: '#app',
store,
data() {
return {
foo: [{
code: 'A'
},
{
code: 'B'
},
{
code: 'C'
}
]
}
},
created() {
},
computed: {
col: {
get() {
return this.$store.state.col;
},
set(newVal) {
this.$store.commit('UPDATE_COL', newVal);
}
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vuex"></script>
<div id="app">
<select v-model="col.code">
<option v-for="i in foo" :value="i.code">{{ i.code }}</option>
</select>
<div v-if="col.code">
My code : {{ col.code }}
</div>
</div>
添加回答
举报