Task properties in Vue.js changed unexpectedly in to-do list app-PHP Chinese Network Q&A
Task properties in Vue.js changed unexpectedly in to-do list app
P粉966979765
P粉966979765 2023-08-26 12:04:57
0
1
382

I have a store that contains states, changes, getters, etc. The status contains the following list of tasks.

const state = { tasks:[{ title: "Get up", completed: false }, { title: "Project 2", completed: false }, ] }

Todo.vue

 

task.vue

 

In the above code, in Task.vue

The problem is in this line. If I remove v-model="task.completed" from the code given above then everything works fine otherwise it throws an error with the message Unexpected mutation of "task" prop

P粉966979765
P粉966979765

reply all (1)
P粉057869348

The problem is that you are trying to change a prop and the corresponding vuex state outside of the mutation. Passing a value to thev-modeldirective will create a two-way data binding. Yourtaskprop refers to an object in the state. Whenq-checkboxchanges the value oftask.completed, the object is updated directly in the state. Instead, you should create a mutation that updates your task:

export const mutations = { updateTask(state, payload){ const task = state.tasks.find(t => t.id === payload.id); if(task) Object.assign(task, payload) } }

Then you can use this mutation

in the template

Also note that the actual prop and event names of theq-checkboxcomponent may vary depending on how you implement it

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!