Can events in the vue parent component be hung directly on the child component?

for example, I pass a method in the parent component, but what should I do if it is executed in the child component? Do not use $emit

Nov.02,2021

There are many

methods. The more common one is to define a Function parameter in the porps of the child component, and the parent component passes the method as a parameter

.

subcomponent code snippet

export default {
  name: 'Children',
  props: {
    parentMethod: Function,
  },
  methods: {
    onClick() {
      this.parentMethod();
    },

parent component code snippet

<template>
  <children :parent-method="testMethod"></children>
</template>
<script>
  export default {
    methods: {
      testMethod() {
        console.log('test');
      },

in this way, when the child component triggers onClick , the parent component's testMethod is called. Although the process is not as clear as $on + $emit , it is still very maintainable

.

ide-inject" rel=" nofollow noreferrer "> provide-inject

Menu