Let's take a look at vue2 first
- Parent component passes parameters to child component
Parent component passes
:
Syntax is actually v-bind to pass parameters
Subcomponent passprops
To get the method passed by the parent component
Yidian Little Knowledge: After receiving the data, the child component cannot directly modify the data of the parent component. Will report an error
// Parent parent passes msg value like child component<template> <Children :datum="'I am the data of the parent component'"></Children> </template> ---------------------------------------------------------------------------------- // Child component receives data passed by parent componentexport default { // Write method 1: Receive with array props:['datum'], // Write method 2: Use object to receive, which can limit the received data type, set default values, verify, etc. props:{ datum:{ type:String, default:'This is the default data' } }, mounted(){ ()// I'm the parent component's data }, }
- The child component passes parameters to the parent component (the method of passing the parent component to the child component is also discussed here)
Parent component passes
@
Syntax is actually v-on to pass the method
Subcomponent pass$emit
To obtain the method of passing the parent component, pass data to the parent component at the same time
<template> <Children @method="method"></Children> </template> <script> import Children from './Children'; export default { components: { Children }, methods: { method(data) { // The data here is the parameter passed by the child component. If the parameters have multiple parameters, you can use the ... syntax to get the parameters (data);// Parameters passed by subcomponents } } }; </script> ---------------------------------------------------------------------------------- // Subcomponent Pass to parent component dataexport default { methods: { childMethod() { // The child component obtains the method passed by the parent component through $emit, and then carries the data to the parent component this.$emit('method',"I am a child component"); } } }
- How to use child components for parent components
In vue2.0, the parent component calls the child component through
$refs
Implemented
// Parent component<template> <Children ref="child"></Children> </template> export default{ import Children from './Children' export default{ components:{ Children }, mounted:{ //Calling child component method, pay attention to distinguishing here that child is the name of ref this.$(val) // Find the child component through $refs and find the method to execute } } }
The above is the communication between the parent components of vue2 child components
vue3
I believe that friends who can understand vue2 should understand the communication between them. Here I will communicate directly between the parent component and the child component.
- Parent component
<template> <Children :title="I am the parent component" ref="childrenRef" @method="methodChildren"></Children > </template> <script lang="ts"> import Children from "./" import { defineComponent, ref } from "vue" export default defineComponent({ components: { Children , }, setup() { let msg = ref("") let childrenRef = ref() // Get instance of child component through ref let fun = () =>{ ()// How to use child components } let methodChildren = (val) => { = val // Here val gets the value passed by the child component } return { msg, methodChildren, } }, }) </script>
- Subcomponents
<template> <!-- Click to call the parent component method --> <button @click="fatherMethod">Click</button> </template> <script lang="ts"> import { defineComponent } from "vue" export default defineComponent({ name: "Children", props: { title: { type: String, }, }, setup(props, {emit}) { const fatherMethod= () => { emit("method", "Passing value to parent component") } const fatherFun= () => { ("I'm a child component method") } return { fatherMethod, } }, }) </script>
The above is the detailed content of the value transfer method between the parent components of vue2 and vue3. For more information about the value transfer of vue2 and vue3 components, please pay attention to my other related articles!