How to bind input field and update vuex state at same time

后端 未结 1 1308
甜味超标
甜味超标 2021-01-06 05:42

Im coming from a React background and it\'s simply enough to set your state from a prop and you could call setState({...}) to update the state, so, with vue / v

相关标签:
1条回答
  • 2021-01-06 06:19

    Yo can use a computed setter

    computed:{
        name:{
            get: function(){ 
                return store.state.name; 
            }, 
            set: function(newName){ 
                store.dispatch('addName',newName); 
            }
        }
    } 
    enter code here
    

    And set the v-model to the computed property name in your <input> tag :

    <input v-model="name" />

    Here is the working jsfiddle

    0 讨论(0)
提交回复
热议问题