Home > Web Front-end > Vue.js > How to use provide/inject in Vue to transfer data between ancestor components and descendant components

How to use provide/inject in Vue to transfer data between ancestor components and descendant components

王林
Release: 2023-06-11 11:36:01
Original
1524 people have browsed it

In Vue, we often need to transfer data between components. When passing data between ancestor components and descendant components, we can use provide/inject provided by Vue.

1. The role of provide/inject

In Vue, provide and inject are a pair of APIs used for data transfer between ancestors and descendants.

Specifically, provide is used to define some data/methods that need to be shared in ancestor components, while inject is used to inject these data/methods into descendant components.

In this way, the data/methods provided in the ancestor component can be used by the descendant component, thus realizing data transfer well.

2. Use of provide/inject

Now, let’s take a look at how to use provide/inject.

In the ancestor component, we can use provide to define the data/methods that need to be shared, for example:

provide: {
  theme: 'blue',
  changeTheme: theme => {
    this.theme = theme
  }
}
Copy after login

In this example, we define a theme variable and a changeTheme method, and They are shared to descendant components through provide.

In descendant components, we can use inject to receive these data/methods, for example:

inject: ['theme', 'changeTheme']
Copy after login

In this example, we receive the two data theme and changeTheme through inject /method.

In this way, we can use the two data/methods theme and changeTheme in descendant components.

this.theme // 获取theme变量的值
this.changeTheme('red') // 改变主题色
Copy after login

It should be noted that when using inject to inject data/methods, we do not have to name them the same as those defined by provide. We can choose a more semantic name as needed.

In addition, it should be noted that when using provide/inject, you need to ensure that the ancestor component is created before the descendant component, otherwise the data/method cannot be injected. This can generally be ensured by the order in which components are created.

3. Sample code

Now, let’s look at a complete sample code to better understand how to use provide/inject.

Parent component:

<template>
  <div>
    <h3>当前主题色为{{theme}}</h3>
    <button @click="changeTheme('blue')">蓝色</button>
    <button @click="changeTheme('green')">绿色</button>
    <button @click="changeTheme('red')">红色</button>
    <hr>
    <child></child>
  </div>
</template>

<script>
import Child from './Child.vue'

export default {
  components: {
    Child
  },
  provide() {
    return {
      theme: this.theme,
      changeTheme: this.changeTheme
    }
  },
  data() {
    return {
      theme: 'blue'
    }
  },
  methods: {
    changeTheme(theme) {
      this.theme = theme
    }
  }
}
</script>
Copy after login

Child component:

<template>
  <div>
    <h3>我是子组件</h3>
    <p>当前主题色为:{{theme}}</p>
    <button @click="changeTheme('blue')">蓝色</button>
    <button @click="changeTheme('green')">绿色</button>
    <button @click="changeTheme('red')">红色</button>
  </div>
</template>

<script>
export default {
  inject: ['theme', 'changeTheme'],
  mounted() {
    console.log(this.theme) // blue
  },
  methods: {
    changeTheme(theme) {
      this.changeTheme(theme)
    }
  }
}
</script>
Copy after login

In this example, we define a theme variable and a changeTheme method in the parent component, and use provide to They are shared with child components.

In the child component, we receive the two data/methods theme and changeTheme through inject, and change the theme color through the changeTheme method.

4. Summary

Using provide/inject can effectively achieve data transfer between ancestor components and descendant components. When using it, we only need to define the data/methods that need to be shared in the ancestor component, and then inject it in the descendant component.

It should be noted that when using provide/inject, you need to ensure that the ancestor component is created before the descendant component, otherwise the data/method cannot be injected.

The above is the detailed content of How to use provide/inject in Vue to transfer data between ancestor components and descendant components. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template