From father to son:1. In the subcomponent tag of the parent component, pass: the data name passed to the subcomponent = "data to be passed"Here, for your reference, I define the data in the parent component as: fatherData , The data that the child component needs to receive is defined as: sonData. // Parent component <template> <div class="about"> {{fatherData}} <!-- From father to son--> <!-- 1. In the subcomponent tag of the parent component, pass: the data name passed to the subcomponent = "data to be passed" --> <children :sonData="fatherData"></children> </div> </template> <script> import children from "@/components/children" import { defineComponent,reactive,toRefs } from "vue"; export default defineComponent({ components:{ children, }, name:"about", setup(){ const state = reactive({ fatherData: "hello" }) return { ...toRefs(state) } } }) </script> 2. Subcomponents are still received through props and used in templatesIn most cases, the data will be passed from the parent component to the child component, and specific functions or request data will be performed based on this data. In the setup hook, the first parameter props can access the data passed by the parent component. Then in the function, the data is operated through: props. The name of the data passed by the parent component. The setup function receives props as its first parameter. The props object is responsive (one-way parent -> child). watchEffect or watch will observe and respond to updates to props. Don't destructure the props object, as that will make it less responsive. During development, the props object is immutable to userspace code, and a warning will be triggered when the user attempts to modify props. // Subcomponent <template> <div class="children"> <!-- 3. Use in subcomponent templates--> {{sonData}} </div> </template> <script> export default { name:"Children", // 2. The subcomponent receives props through props:["sonData"], setup(props){ function childrenbut(){ // props.sonData can access the data passed by the parent component console.log(props.sonData); } return { childrenbut } } } </script> Son to Father:1. The subcomponent triggers the event through the second parameter of setup, context.emit, to achieve child-to-parent transmission context context object. // Subcomponent <template> <div class="children"> {{sonData}} <!-- 1. Trigger event--> <button @click="childrenbut">Child component button</button> </div> </template> <script> export default { name:"Children", setup(props,context){ function childrenbut(){ console.log(context); // 2. Use context.emit to pass data from child to parent. // The first parameter is the method name, and the second parameter is the data to be passed. context.emit("change",'world') } return { childrenbut, } } } </script> context can also be written in the form of a structure // Subcomponent <script> export default { name:"Children", // After passing the structure, write emit directly {emit} setup(props,{emit}){ function childrenbut(){ // Omit context.emit emit("change",'world') } return { childrenbut, } } } </script> SummarizeIn vue3, whether it is parent-to-child or child-to-parent, it is actually the same as in vue2. The ideas are mostly the same, but the difference is that vue3 needs to call various hooks to pass parameters You may also be interested in:
|
>>: Building a selenium distributed environment based on docker
Scenario: As the amount of data increases, the di...
This article explains how to install MySQL from a...
Table of contents 1. Through HBuilderX visual int...
Bugs As shown in the figure, I started to copy th...
Table of contents Overall Effect Listen for conta...
Table of contents Preface Scenario simulation Sum...
How much do you know about HTML? If you are learni...
body{font-size:12px; font-family:"宋体";}...
Copy code The code is as follows: <select> ...
After installing Navicat The following error may ...
1. Background Recently, some friends encountered ...
Table of contents 1. Shallow copy 1. Object.assig...
Preface Creating shortcuts in Linux can open appl...
When building a B/S system interface, you often en...
This article shares the specific code of the WeCh...