Detailed explanation of non-parent-child component communication in Vue3

Detailed explanation of non-parent-child component communication in Vue3

The business scenario is that it is not the parent-child components that communicate

File Directory

insert image description here

First method

App.vue

<template>
  <div>
    <Home></Home>
    <button @click="addName">Add name</button>
  </div>
</template>
 <script>
import Home from "./Home.vue";
import { computed } from "vue";
export default {
  name: "App",
  components:
    Home,
  },
  provide() {
    return {
      name: "Zhang San",
      age: 23,
      length: computed(() => this.names.length),
    };
  },
  data() {
    return {
      names: ["Zhang San", "Li Si", "Wang Wu"],
    };
  },
  methods: {
    addName() {
      this.names.push("fuck you");
      console.log("hhhh");
    },
  },
};
</script>
 <style scoped>
</style>

Home.vue

<template>
  <div>
    <div>I am home</div>
    <home-content></home-content>
  </div>
</template>
 <script>
import HomeContent from "./HomeContent.vue";
export default {
  name: "Home",
  components:
    HomeContent,
  },
};
</script>
 <style scoped>
</style>

HomeContent.vue

<template>
  <div>HomeContent:{{ name }}--{{ age }}---{{ length }}</div>
</template>
 <script>
export default {
  inject: ["name", "age", "length"],
};
</script>
 <style lang="scss" scoped>
</style>

Use Provide function and Inject function in vue3

In fact, we have learned Provide and Inject before. Composition API can also replace the previous Provide and Inject options.

We can provide data through provide:

Provide can pass in two parameters:

name : the provided attribute name;

value : the attribute value provided;

insert image description here

In the descendant components, you can use inject to inject the required properties and corresponding values:

You can use inject to inject the required content;

Inject can pass in two parameters:

The name of the property to be injected;

default value;

insert image description here

Responsiveness of data

To add responsiveness between provided values ​​and injected values, we can use ref and reactive when providing values.

Modifying Responsive Properties

If we need to modify responsive data, it is best to modify it where the data is provided: we can share the modification method and call it in descendant components;

Notice

If our subcomponent should be a state that can only be used and cannot be modified

insert image description here

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Summary of non-parent-child component value transfer knowledge points in Vue component communication
  • Vue non-parent-child component communication problem and solution
  • Analysis of non-parent-child component communication in event bus in Vue
  • How to use Bus.js in Vue2 to achieve non-parent-child component communication
  • Detailed explanation of Vue non-parent-child component communication
  • Do you understand the communication between non-parent-child components in Vue?

<<:  SQL Optimization Tutorial: IN and RANGE Queries

>>:  How to implement HTML to detect that input is complete and automatically fill in the next content

Recommend

CSS complete parallax scrolling effect

1. What is Parallax scrolling refers to the movem...

How to use vue3 to build a material library

Table of contents Why do we need a material libra...

Detailed explanation of using echarts map in angular

Table of contents Initialization of echart app-ba...

4 ways to implement routing transition effects in Vue

Vue router transitions are a quick and easy way t...

How to understand the difference between computed and watch in Vue

Table of contents Overview computed watch monitor...

New ways to play with CSS fonts: implementation of colored fonts

What if you designers want to use the font below ...

express project file directory description and detailed function description

app.js: startup file, or entry file package.json:...

Detailed explanation of the functions and usage of MySQL common storage engines

This article uses examples to illustrate the func...

Analysis of different MySQL table sorting rules error

The following error is reported when MySQL joins ...

5 basic skills of topic page design (Alibaba UED Shanmu)

This topic is an internal sharing in the second h...

Solution to overflow:hidden failure in CSS

Cause of failure Today, when I was writing a caro...

Vue implements the question answering function

1. Request answer interface 2. Determine whether ...

Detailed tutorial on deploying Apollo custom environment with docker-compose

Table of contents What is the Apollo Configuratio...

Analysis of MySQL general query log and slow query log

The logs in MySQL include: error log, binary log,...