How to understand that Vue applications pass events down and up by prop

how to understand that Vue applications are prop passing events down and up

official documents say so, and there are also examples of official code as follows:
counterexample 1

Vue.component("TodoItem", {
  props: {
    todo: {
      type: Object,
      required: true
    }
  },
  template: "<input v-model="todo.text">"
})

counterexample 2

Vue.component("TodoItem", {
  props: {
    todo: {
      type: Object,
      required: true
    }
  },
  methods: {
    removeTodo () {
      var vm = this
      vm.$parent.todos = vm.$parent.todos.filter(function (todo) {
        return todo.id !== vm.todo.id
      })
    }
  },
  template: `
    <span>
      {{ todo.text }}
      <button @click="removeTodo">
        X
      </button>
    </span>
  `
})

positive example 1

Vue.component("TodoItem", {
  props: {
    todo: {
      type: Object,
      required: true
    }
  },
  template: `
    <input
      :value="todo.text"
      @input="$emit("input", $event.target.value)"
    >
  `
})

positive example 2

Vue.component("TodoItem", {
  props: {
    todo: {
      type: Object,
      required: true
    }
  },
  template: `
    <span>
      {{ todo.text }}
      <button @click="$emit("delete")">
        X
      </button>
    </span>
  `
})

there are still many examples of this kind on the Internet, but most of them are code, so I don"t quite understand. So I"m here to ask you for advice.

May.31,2021

in fact, the subject did not make your problem clear. But I think I can guess, so answer it.

in fact, this is a common problem in object-oriented (similar to the componentization of Vue): how to transfer state between instances. That is, if you want to use B in A, how to pass some values back and forth. It is also easy to write B.foo = 'bar' directly in A, so how does B send it back?

The real difficulty of

is how to ensure that each class can be reused. For example, counterexample 2 above. It directly modifies the properties of the parent component, so it requires:

  1. must have a parent component
  2. parent component must have a corresponding property
  3. the type of the property corresponding to the parent component must meet the requirements of the child component (in this case, an array)

just a todo-list applet looks fine, but in a large product, if each class is so tightly coupled to other classes, it is simply impossible to maintain and test. Therefore, the method commonly adopted by everyone now is:

  1. because the parent component knows the child component, the parent component can directly manipulate the child component
  2. the child component notifies the parent component to take an action through the event

in counterexample 1, Vue uses implicit two-way data binding, that is, using getter/setter instead of direct assignment. So the v-model practice will directly modify the variables of the parent component and to a large extent lead to strong coupling between components, so it is recommended to avoid it as much as possible.

Menu