I started a note taking project with vue-cli and vuex. I have setup everything correctly and everything works fine besides when I add a new note in the vuex store - notelist, it gets added which is fine but when I change the value in the input field my vuex store value also gets updated without committing anything.
here is the code -
<template>
<div>
{{title}}
<input type="text" v-model="note.msg">
<button v-on:click="addNote()">ADD</button>
</div>
</template>
<script>
module.exports = {
data() {
return {
title:'Add node',
note: {
msg: this.msg,
avatar:'wolf'
}
}
},
methods: {
addNote(){
this.$store.dispatch("updateNotes",this.note);
}
},
}
</script>
<style scoped>
</style>
When I change the input value after adding my notes, it gets updated in vuex store and also in the other component where I am displaying the list.
what I have tried is this - Instead of making the note.msg as v-model I changed it to msg and data object and computed property has been updated accordingly and it fixed my problem -
<template>
<div>
{{title}}
<input type="text" v-model="msg">
<button v-on:click="addNote()">ADD</button>
</div>
</template>
<script>
module.exports = {
data() {
return {
title:'Add node',
msg:''
}
},
methods: {
addNote(){
let note = {
msg: this.msg,
avatar:'wolf'
};
this.$store.dispatch("updateNotes",note);
}
},
}
</script>
<style scoped>
</style>
Technically I don't have any problem, just I couldn't get my head around this. I didn't expect that to happen. I've expected the second behaviour not the first one.
EDIT
action.js
const updateNotes = (context, payload) => {
context.commit('updateNotes', payload);
}
export default {
updateNotes,
};
mutation.js
const updateNotes = (state, data) => {
state.notes.push(data);
}
export default {
updateNotes
};
Those are the mutations and actions in my project.
