通过Vuex实现Input的双向绑定
yarn add vuex
import store from './store'
new Vue({
store,
render: h => h(App)
}).$mount('#main')
import Vue from 'vue'
import Vuex from 'vuex'
const store = new Vuex.store({
state:{
inputVal: 'Hello'
},
mutations:{
setInput (state, newVal) {
state.inputVal = newVal
}
}
})
<template>
<div>
<input v-model = 'myVal'>
</div>
</template>
<script>
export default {
computed: {
myVal (e) {
get() {
return this.$store.state.inputVal
},
set(newVal) {
this.$store.commit('setInput', newVal)
}
}
}
}
</script>
网友评论