0
votes

i have a problem with my props editToTask :

app.js:42491 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "taskToEdit"

component TaskComponent.vue

<template>
...
        <button
          type="button"
          class="btn btn-secondary"
          data-toggle="modal"
          data-target="#editModal"
          @click="getTask(task.id)"
        >Editer</button>
      </li>
      <edit-task v-bind:taskToEdit="taskToEdit"></edit-task>
...
</template>

<script>
export default {
  data() {
    return {
      tasks: {},
      taskToEdit: "",
    };
  },

  methods: {

    getTask(id) {
      axios
        .get("http://localhost:3000/tasks/edit/" + id)
        .then((res) => (this.taskToEdit = res.name))
        .catch((error) => console.log(error));
    },
};
</script>

component EditTaskComponent :

<template>
...
            <form>
              <div class="form-group">
                <label for>Nom des tâches</label>
                <textarea name="name" id="name" rows="4" class="form-control" v-model="taskToEdit"></textarea>
              </div>
            </form>
...
</template>

<script>
export default {
  props: ["taskToEdit"],
};
</script>
1

1 Answers

1
votes

Your problem can be solved easier, you should not change props. v-model solves this pretty well you should look into component v-model.

Firstly you can make your child component work with v-model, use a prop named value instead in the child component. Which represent the task value.

<script>
export default {
  props: ["value"],
};
</script>

Secondly avoid using v-model to mutate the prop. Instead emit an input event.

<textarea name="name" id="name" rows="4" class="form-control" :value="value" @input="$emit('input', $event.target.value)"></textarea>

In your parent component you can use v-model like this now.

<edit-task v-model="taskToEdit"></edit-task>