The business scenario is that it is not the parent-child components that communicate File Directory First methodApp.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: 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; Responsiveness of data
Modifying Responsive PropertiesIf 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 SummarizeThis 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:
|
<<: SQL Optimization Tutorial: IN and RANGE Queries
Operating system: Ubuntu 17.04 64-bit MySQL versi...
Today, I will answer these newbie questions: Build...
Why do we need permission management? 1. Computer...
Preface: As a junior programmer, I dream of build...
Table of contents DML statements 1. Insert record...
There is only one solution, that is to change the...
Table of contents Initialize computed Dependency ...
1. Let’s take a look at the effect first Data ret...
Background: I wonder if you have noticed that if ...
Table of contents 1. Introduction 2. Configure My...
Table of contents 1. Set Deduplication 2. Double ...
Table of contents 1. What is the life cycle 2. Th...
What are the benefits of learning HTML? 1: Easily...
1. Compatibility As shown below: The compatibilit...
1. Introduction Recently, I often encounter devel...