Home >Web Front-end >Vue.js >What are the implementation methods of Vue component communication?

What are the implementation methods of Vue component communication?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2023-07-17 20:22:371494browse

Vue.js is a popular front-end framework that provides a component-based development approach to build complex interactive applications. In Vue, communication between components is a very important part. It can help us share and coordinate data and improve development efficiency. This article will introduce several implementation methods of component communication in Vue, and attach corresponding code examples.

  1. props and $emit methods

props and $emit are the most basic and commonly used component communication methods in Vue. props are used to pass data from the parent component to the child component, and the $emit method is used to trigger custom events in the child component and pass data to the parent component.

Example:

// Parent component

<script> <br> export default {</script>

data() {
  return {
    message: 'Hello Vue!'
  }
},
methods: {
  handleEvent(data) {
    console.log(data);
  }
}

}

// Subcomponent

<script><br> export default {</script>

props: ['message'], // 接收父组件传递的数据
methods: {
  sendMessage() {
    this.$emit('my-event', 'Hello Parent!'); // 触发自定义事件,并传递数据给父组件
  }
}

}

  1. $parent and $children properties

The $parent and $children properties are used inside a component to access instances of its parent or child components.

Example:

// Parent component

<script><br> import childComponent from './ChildComponent.vue';</script>

export default {

components: {
  'child-component': childComponent
},
mounted() {
  console.log(this.$children); // 访问子组件实例
}

}

// Child component


export default {

mounted() {
  console.log(this.$parent); // 访问父组件实例
}

}

  1. $refs attribute

$refs attribute can be found in The parent component obtains the instance of the child component through the ref attribute, thereby directly operating the properties and methods of the child component.

Example:

// Parent component

<script><br> export default {</script>

mounted() {
  console.log(this.$refs.childRef); // 获取子组件实例
  this.$refs.childRef.sendMessage(); // 调用子组件的方法
}

}

// Subcomponent

<script><br> export default {</script>

methods: {
  sendMessage() {
    console.log('Hello Parent!');
  }
}

}

These are several implementation methods for Vue component communication. In actual development, choosing the appropriate communication method according to specific needs and scenarios can effectively improve development efficiency and code quality. I hope this article will help you understand Vue component communication.

The above is the detailed content of What are the implementation methods of Vue component communication?. 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