Absolutely, it is possible to achieve this. To begin, set up your Vuex store to handle the value and mutation:
store
state: {
amount: null
},
mutations: {
SET_AMOUNT(state, payload) {
state.amount = payload;
}
}
You will need a method to trigger the mutation when the model changes and update the model if the state changes. Utilizing a computed setter can effectively manage both directions and prevent any improper mutation of the Vuex state within the component:
Add a computed
property in the component:
component
computed: {
amount: {
get() { return this.$store.state.amount },
set(value) { this.$store.commit('SET_AMOUNT', value) }
}
}
This approach will seamlessly integrate with the template as described in your initial post.