Passing values between Vue3 parent-child components

In vue3, the way the child component uses the value of the parent component has changed. After receiving it with props, it cannot be called through this.xxx in the setup.

Let's take a look at the value passing between vue2 parent and child

father to son


   
   
    
    
  1. <template>
  2. <div>
  3. < h1 > parent component </ h1 >
  4. <hr />
  5. <son :value="parentValue"> </son>
  6. </div>
  7. </template>
  8. <script>
  9. import son from './components/son.vue'
  10. export default {
  11. components: { son },
  12. data () {
  13. return {
  14. parentValue : 'The value passed from the parent to the child'
  15. }
  16. }
  17. }
  18. </script>

Subcomponents receive via props


   
   
    
    
  1. <template>
  2. <div>
  3. <p> Subcomponent </p> _ _ _ _
  4. <div>{ { value }} </div>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. props: [ 'value']
  10. }
  11. </script>

child's father


   
   
    
    
  1. <template>
  2. <div>
  3. < h1 > parent component </ h1 >
  4. <div>{ { parentValue }} </div>
  5. <hr />
  6. <son @handle="handel"> </son>
  7. </div>
  8. </template>
  9. <script>
  10. import son from './components/son.vue'
  11. export default {
  12. components: { son },
  13. data () {
  14. return {
  15. parentValue : 'The value passed from the parent to the child'
  16. }
  17. },
  18. methods: {
  19. handel (value) {
  20. this. parentValue = value
  21. }
  22. }
  23. }
  24. </script>

The child component triggers the event in the parent component to pass the value to the parent component through $emit


   
   
    
    
  1. <template>
  2. <div>
  3. <p> Subcomponent </p> _ _ _ _
  4. < button @ click = "fn" > Click to pass value to parent component </ button >
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. methods: {
  10. fn () {
  11. this .$emit( ​​'handle' , 'The value passed from the child to the parent' )
  12. }
  13. }
  14. }
  15. </script>

Next, look at the passing value of vue3

father to son


   
   
    
    
  1. <template>
  2. <div>
  3. < h1 > parent component </ h1 >
  4. <hr />
  5. <son :value="parentValue"> </son>
  6. </div>
  7. </template>
  8. <script lang="ts">
  9. import { defineComponent, ref } from 'vue'
  10. import son from './components/son.vue'
  11. export default defineComponent({
  12. components: { son },
  13. setup () {
  14. const parentValue = ref ( 'The value passed from the parent component to the child component' )
  15. return { parentValue }
  16. }
  17. })
  18. </script>

Since there is no this in the setup of vue3, we want to use the value passed from the parent component in the setup, with the help of the first parameter of the setup function


   
   
    
    
  1. <template>
  2. <div>
  3. <p> Subcomponent </p> _ _ _ _
  4. <div>{ { value }} </div>
  5. </div>
  6. </template>
  7. <script lang="ts">
  8. import { defineComponent } from 'vue'
  9. export default defineComponent({
  10. props: [ 'value'],
  11. setup (props, context) {
  12. console . log (props. value ) // the value passed from the parent component to the child component
  13. }
  14. })
  15. </script>

child's father


   
   
    
    
  1. <template>
  2. <div>
  3. < h1 > parent component </ h1 >
  4. { { parentValue }}
  5. <hr />
  6. <son @handle="handle"> </son>
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, ref } from 'vue'
  11. import son from './components/son.vue'
  12. export default defineComponent({
  13. components: { son },
  14. setup () {
  15. let parentValue = ref ( 'The value passed from the parent component to the child component' )
  16. const handle = value => {
  17. parentValue. value = value
  18. }
  19. return { parentValue, handle }
  20. }
  21. })
  22. </script>

The child triggers the method passing value in the parent component through emit


   
   
    
    
  1. <template>
  2. <div>
  3. <p> Subcomponent </p> _ _ _ _
  4. < button @ click = "fn" > Click to change the value of the parent component </ button >
  5. </div>
  6. </template>
  7. <script lang="ts">
  8. import { defineComponent, ref } from 'vue'
  9. export default defineComponent({
  10. setup (props, context) {
  11. const fn = ( ) => {
  12. context.emit ( 'handle' , 'The value passed from the child component to the parent component' )
  13. }
  14. return { fn }
  15. }
  16. })
  17. </script>

provide and inject 

Vue3 also provides us with provide and inject. We can expose properties or methods through provide in the parent component, and the child component accepts it through inject, and as long as it is a descendant of the parent component, it can receive the value exposed by the parent component through inject.


   
   
    
    
  1. <template>
  2. <div>
  3. < h1 > parent component </ h1 >
  4. { { parentValue }}
  5. <hr />
  6. <son> </son>
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, provide, ref } from 'vue'
  11. import son from './components/son.vue'
  12. export default defineComponent({
  13. components: { son },
  14. setup () {
  15. // pass value
  16. let parentValue = ref ( 'value in parent' )
  17. provide( 'parentValue', parentValue)
  18. // pass method
  19. const handle = ( sonValue: any ) => {
  20. parentValue. value = sonValue
  21. }
  22. provide( 'handle', handle)
  23. return { parentValue }
  24. }
  25. })
  26. </script>

   
   
    
    
  1. <template>
  2. <div>
  3. <p> Subcomponent </p> _ _ _ _
  4. <div>{ { value }} </div>
  5. < button @ click = "fn" > Click to modify the value of the parent component </ button >
  6. </div>
  7. </template>
  8. <script lang="ts">
  9. import { defineComponent, inject } from 'vue'
  10. export default defineComponent({
  11. setup () {
  12. const value = inject ( 'parentValue' ) // use the value passed from the parent component
  13. const handle: any = inject( 'handle')
  14. // Modify the value of the parent component
  15. const fn = ( ) => {
  16. handle ( 'Modify the value of the parent component' )
  17. }
  18. return { value, fn }
  19. }
  20. })
  21. </script>

In vue3, the way the child component uses the value of the parent component has changed. After receiving it with props, it cannot be called through this.xxx in the setup.

Let's take a look at the value passing between vue2 parent and child

father to son


   
   
  
  
  1. <template>
  2. <div>
  3. < h1 > parent component </ h1 >
  4. <hr />
  5. <son :value="parentValue"> </son>
  6. </div>
  7. </template>
  8. <script>
  9. import son from './components/son.vue'
  10. export default {
  11. components: { son },
  12. data () {
  13. return {
  14. parentValue : 'The value passed from the parent to the child'
  15. }
  16. }
  17. }
  18. </script>

Subcomponents receive via props


   
   
  
  
  1. <template>
  2. <div>
  3. <p> Subcomponent </p> _ _ _ _
  4. <div>{ { value }} </div>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. props: [ 'value']
  10. }
  11. </script>

child's father


   
   
  
  
  1. <template>
  2. <div>
  3. < h1 > parent component </ h1 >
  4. <div>{ { parentValue }} </div>
  5. <hr />
  6. <son @handle="handel"> </son>
  7. </div>
  8. </template>
  9. <script>
  10. import son from './components/son.vue'
  11. export default {
  12. components: { son },
  13. data () {
  14. return {
  15. parentValue : 'The value passed from the parent to the child'
  16. }
  17. },
  18. methods: {
  19. handel (value) {
  20. this. parentValue = value
  21. }
  22. }
  23. }
  24. </script>

The child component triggers the event in the parent component to pass the value to the parent component through $emit


   
   
  
  
  1. <template>
  2. <div>
  3. <p> Subcomponent </p> _ _ _ _
  4. < button @ click = "fn" > Click to pass value to parent component </ button >
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. methods: {
  10. fn () {
  11. this .$emit( ​​'handle' , 'The value passed from the child to the parent' )
  12. }
  13. }
  14. }
  15. </script>

Next, look at the passing value of vue3

father to son


   
   
  
  
  1. <template>
  2. <div>
  3. < h1 > parent component </ h1 >
  4. <hr />
  5. <son :value="parentValue"> </son>
  6. </div>
  7. </template>
  8. <script lang="ts">
  9. import { defineComponent, ref } from 'vue'
  10. import son from './components/son.vue'
  11. export default defineComponent({
  12. components: { son },
  13. setup () {
  14. const parentValue = ref ( 'The value passed from the parent component to the child component' )
  15. return { parentValue }
  16. }
  17. })
  18. </script>

Since there is no this in the setup of vue3, we want to use the value passed from the parent component in the setup, with the help of the first parameter of the setup function


   
   
  
  
  1. <template>
  2. <div>
  3. <p> Subcomponent </p> _ _ _ _
  4. <div>{ { value }} </div>
  5. </div>
  6. </template>
  7. <script lang="ts">
  8. import { defineComponent } from 'vue'
  9. export default defineComponent({
  10. props: [ 'value'],
  11. setup (props, context) {
  12. console . log (props. value ) // the value passed from the parent component to the child component
  13. }
  14. })
  15. </script>

child's father


   
   
  
  
  1. <template>
  2. <div>
  3. < h1 > parent component </ h1 >
  4. { { parentValue }}
  5. <hr />
  6. <son @handle="handle"> </son>
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, ref } from 'vue'
  11. import son from './components/son.vue'
  12. export default defineComponent({
  13. components: { son },
  14. setup () {
  15. let parentValue = ref ( 'The value passed from the parent component to the child component' )
  16. const handle = value => {
  17. parentValue. value = value
  18. }
  19. return { parentValue, handle }
  20. }
  21. })
  22. </script>

The child triggers the method passing value in the parent component through emit


   
   
  
  
  1. <template>
  2. <div>
  3. <p> Subcomponent </p> _ _ _ _
  4. < button @ click = "fn" > Click to change the value of the parent component </ button >
  5. </div>
  6. </template>
  7. <script lang="ts">
  8. import { defineComponent, ref } from 'vue'
  9. export default defineComponent({
  10. setup (props, context) {
  11. const fn = ( ) => {
  12. context.emit ( 'handle' , 'The value passed from the child component to the parent component' )
  13. }
  14. return { fn }
  15. }
  16. })
  17. </script>

provide and inject 

Vue3 also provides us with provide and inject. We can expose properties or methods through provide in the parent component, and the child component accepts it through inject, and as long as it is a descendant of the parent component, it can receive the value exposed by the parent component through inject.


   
   
  
  
  1. <template>
  2. <div>
  3. < h1 > parent component </ h1 >
  4. { { parentValue }}
  5. <hr />
  6. <son> </son>
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, provide, ref } from 'vue'
  11. import son from './components/son.vue'
  12. export default defineComponent({
  13. components: { son },
  14. setup () {
  15. // pass value
  16. let parentValue = ref ( 'value in parent' )
  17. provide( 'parentValue', parentValue)
  18. // pass method
  19. const handle = ( sonValue: any ) => {
  20. parentValue. value = sonValue
  21. }
  22. provide( 'handle', handle)
  23. return { parentValue }
  24. }
  25. })
  26. </script>

   
   
  
  
  1. <template>
  2. <div>
  3. <p> Subcomponent </p> _ _ _ _
  4. <div>{ { value }} </div>
  5. < button @ click = "fn" > Click to modify the value of the parent component </ button >
  6. </div>
  7. </template>
  8. <script lang="ts">
  9. import { defineComponent, inject } from 'vue'
  10. export default defineComponent({
  11. setup () {
  12. const value = inject ( 'parentValue' ) // use the value passed from the parent component
  13. const handle: any = inject( 'handle')
  14. // Modify the value of the parent component
  15. const fn = ( ) => {
  16. handle ( 'Modify the value of the parent component' )
  17. }
  18. return { value, fn }
  19. }
  20. })
  21. </script>

Guess you like

Origin blog.csdn.net/weixin_64310738/article/details/129034421