What is the difference between vue property methods and event calls?

suppose you have the following example:

1, parent component

<template>
    <test1 :handleChange="onChange" />
    <test2 @handleChange="onChange" />
</template>
<script>
...
methods: {
    onChange(data) {...}
}
...
</script>

2, test1 components

<template>
    <div @click="onChange"></div>
</template>
<script>
...
props: {
    handleChange: Function
},
methods: {
    onChange() {
        this.handleChange(123)
    }
}
...
</script>

3, test2 components

<template>
    <div @click="onChange"></div>
</template>
<script>
...
methods: {
    onChange() {
        this.$emit("handleChange", 123)
    }
}
...
</script>

question:

both the test1 component and the test2 component delegate events externally, but one is in the form of property methods used, and the other is in the form of delegated events. When the parent component makes the call, it finds that both of them can do the same thing.
what is the difference between them?

< hr >

the difference I know so far is:
1. Using the form of attributes is equivalent to passing external methods to subcomponents to make calls, while delegating events is the way in which subcomponents report an event to the outside and receive and execute it from the outside.

Apr.24,2021

from the point of view of the execution result, there is little difference; from the code architecture level, the "event-listening" mechanism is recommended. Because the latter helps to decouple between parent and child components, that is, the parent component does not have to be the child component, and the child component does not require the parent component to pass a handler or report an error.


the first is a typical parent-child data transfer. The advantage is that you can easily see the function call relationship. The disadvantage is that if the component is not a parent-child relationship, it needs to be passed on layer by layer, which is very uncomfortable. In addition, the coupling is more serious. The parent and child components do not meet the principle of minimum knowledge. However, principles do not have to be followed. For example, jquery's prop method, which can both obtain properties and set data, does not meet the principle of single responsibility.

the second is to publish subscriber mode. More conducive to decoupling. It is also not required that the components that accept and send events have a parent-child relationship. The disadvantage is that once this model is misused, it will cause serious confusion, and it is easy to confuse the flow and data direction of the system.


Please use the second. Decoupling is necessary for component design. The first one is not highly available.


There are certain principles in the design of

components.

the parent and child components here, the parent component's own methods, it is best to control in the parent component, easy to read and controllable.
in addition, the methods of the parent component are executed in the parent component, so the response methods basically operate on the properties of the parent component, and the child components cannot manipulate the properties of the parent component.

Menu