Home Web Front-end Vue.js Method functions in Vue3: Master the method of communication between Vue3 components

Method functions in Vue3: Master the method of communication between Vue3 components

Jun 18, 2023 pm 02:13 PM
vue Component communication method function

Vue3 is one of the most popular front-end frameworks currently. It is highly praised for its powerful features and simple and easy-to-use API. Vue3 provides many ways to organize and interact with various components, including inter-component communication, state management, dynamic components, etc. In Vue3, we can use some method functions to implement communication between components. Let us master these methods.

  1. props

props is an important feature of Vue3. It is a way to define the properties of components and transfer data. If you need to pass data from a component to its child components, you can use props. You can specify an array in the props option in the child component, which contains the properties you want to accept. When you pass properties from a parent component, these properties are automatically passed to and available in the child component. The following is a simple example:

<template>
  <div>
    <Child :message="message" />
  </div>
</template>
<script>
import Child from './Child.vue'
export default {
  components: { Child },
  data() {
    return {
      message: 'Hello World!'
    }
  }
}
</script>
Copy after login

In the Child component, we can receive data through props:

<template>
  <div>
    <p>{{ message }}</p>
  </div>
</template>
<script>
export default {
  props: {
    message: {
      type: String,
      required: true
    }
  }
}
</script>
Copy after login
Copy after login

Note that the type of each attribute needs to be specified in the props option, so that This ensures that the data type passed into the child component is correct.

  1. emit

emit is another commonly used inter-component communication method in Vue3. When you need to trigger an event in a child component, you can use the emit method. In the parent component, you can listen to this event and perform some operations. The following is a simple example:

<template>
  <div>
    <Child @alert="showAlert" />
  </div>
</template>
<script>
import Child from './Child.vue'
export default {
  components: { Child },
  methods: {
    showAlert(msg) {
      alert(msg)
    }
  }
}
</script>
Copy after login

In the Child component, we can use $emit to trigger an event:

<template>
  <div>
    <button @click="onClick">Click Me</button>
  </div>
</template>
<script>
export default {
  methods: {
    onClick() {
      this.$emit('alert', 'Hello World!')
    }
  }
}
</script>
Copy after login

When the user clicks the button, the child component will trigger an alert event, and pass a message to the parent component.

  1. provide/inject

provide/inject is another inter-component communication method provided by Vue3. It is slightly different from props and emit in that it allows you to provide some data to the child component. Child components can receive this data through the inject option. The following is a simple example:

<template>
  <div>
    <Child />
  </div>
</template>
<script>
import Child from './Child.vue'
export default {
  provide: {
    message: 'Hello World!'
  },
  components: { Child }
}
</script>
Copy after login

In the Child component, we can use the inject option to receive this data:

<template>
  <div>
    <p>{{ message }}</p>
  </div>
</template>
<script>
export default {
  inject: ['message']
}
</script>
Copy after login

Note that the name of the data that needs to be received is specified in the inject option. This way you can use it directly in child components.

  1. $parent / $children

$parent and $children are two other tools for communication between components provided by Vue3. $parent is used from a child component to access properties or methods in its parent component, while $children is used from a parent component to access properties or methods in its child components. Since these two options are provided in Vue3, they may be abandoned in future versions of Vue3.

  1. $attrs / $listeners

$attrs and $listeners are two magical options provided by Vue3. The $attrs option provides the component with all the attributes passed to it, and they can be used with the child component's props option as follows:

<template>
  <div>
    <Child v-bind="$attrs" />
  </div>
</template>
<script>
import Child from './Child.vue'
export default {
  components: { Child },
  data() {
    return {
      message: 'Hello World!'
    }
  },
  mounted() {
    console.log(this.$attrs) // { message: "Hello World!" }
  }
}
</script>
Copy after login

In the Child component:

<template>
  <div>
    <p>{{ message }}</p>
  </div>
</template>
<script>
export default {
  props: {
    message: {
      type: String,
      required: true
    }
  }
}
</script>
Copy after login
Copy after login

In In the above example, the subcomponent can use the $attrs option to receive and define the corresponding props options. The

$listeners option provides the component with all event listeners in the parent component. This will allow you to use these event listeners in child components. As shown below:

<template>
  <div>
    <button v-on="$listeners">Click Me</button>
  </div>
</template>
<script>
export default {
  mounted() {
    console.log(this.$listeners) // { 'click': [f1] }
  }
}
</script>
Copy after login

When the user clicks the button, the event listener in the parent component will be fired.

Summary

This article lists the commonly used inter-component communication methods in Vue3. These methods include: props, emit, provide/inject, $parent/$children, and $attrs/$listeners. These methods can help you better organize and interact with various components, improve your development efficiency and improve user experience. In practice, you may need to use multiple methods simultaneously to achieve your business goals, so make sure you are aware of the options and know when and where they apply.

The above is the detailed content of Method functions in Vue3: Master the method of communication between Vue3 components. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

What does vue multi-page development mean? What does vue multi-page development mean? Apr 07, 2025 pm 11:57 PM

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses &lt;router-link to=&quot;/&quot; component window.history.back(), and the method selection depends on the scene.

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

There are three ways to refer to JS files in Vue.js: directly specify the path using the &lt;script&gt; tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

How to use vue traversal How to use vue traversal Apr 07, 2025 pm 11:48 PM

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values ​​for each element; and the .map method can convert array elements into new arrays.

How to jump to the div of vue How to jump to the div of vue Apr 08, 2025 am 09:18 AM

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.

See all articles