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

Some Linux file permission management methods you may not know

Why do we need permission management? 1. Computer...

How to quickly build a static website on Alibaba Cloud

Preface: As a junior programmer, I dream of build...

MySQL Basic Tutorial: Detailed Explanation of DML Statements

Table of contents DML statements 1. Insert record...

Detailed explanation of the solution to docker-compose being too slow

There is only one solution, that is to change the...

Detailed explanation of the cache implementation principle of Vue computed

Table of contents Initialize computed Dependency ...

Python Flask WeChat applet login process and login api implementation code

1. Let’s take a look at the effect first Data ret...

Exploration and correction of the weird behavior of parseInt() in js

Background: I wonder if you have noticed that if ...

MySQL Router implements MySQL read-write separation

Table of contents 1. Introduction 2. Configure My...

Detailed discussion of several methods for deduplicating JavaScript arrays

Table of contents 1. Set Deduplication 2. Double ...

Life cycle and hook functions in Vue

Table of contents 1. What is the life cycle 2. Th...

HTML is something that web page creators must learn and master.

What are the benefits of learning HTML? 1: Easily...

Understand the use of CSS3's all attribute

1. Compatibility As shown below: The compatibilit...

A detailed analysis of the murder caused by a misplaced double quote in MySQL

1. Introduction Recently, I often encounter devel...