Home >Web Front-end >JS Tutorial >How to use parent component to call child component events in Vue

How to use parent component to call child component events in Vue

亚连
亚连Original
2018-06-05 11:13:051946browse

Below I will share with you an article about Vue parent components calling child component event methods. It has a good reference value and I hope it will be helpful to everyone.

Vue parent component passes events/calls events to child components

It is not about passing data (props), it is applicable to Vue 2.0

Method 1: The child component listens to the method sent by the parent component

Method 2: The parent component calls the child component method

Child component:

export default {
  mounted: function () {
   this.$nextTick(function () {
    this.$on('childMethod', function () {
     console.log('监听成功')
    })
   })
  },
  methods {
    callMethod () {
     console.log('调用成功')
    }
  }
}

Parent component:

<child ref="child" @click="click"></child>

export default {
  methods: {
   click () {
   this.$refs.child.$emit(&#39;childMethod&#39;) // 方法1
   this.$refs.child.callMethod() // 方法2
  },
  components: {
   child: child
  }
}

That’s me I compiled it for everyone, I hope it will be helpful to everyone in the future.

Related articles:

How to create components in Vue

How to dynamically add li tags and add attributes and bindings in jQuery Defined event method

How to implement default style modification in elementui

The above is the detailed content of How to use parent component to call child component events in Vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn