Use of inject and provide in vue3 (dependency injection)

Parent component (TestOne.vue): The parent component passes the property name and value

<template>
    <div>依赖注入练习</div>
    <TestOneSon></TestOneSon>
</template>

<script setup>
import { provide, ref } from "vue"
import TestOneSon from "../components/TestOneSon.vue";
const aa = ref(10)
provide("info1", aa)
</script>

Child component (TestOneSon.vue): The child component injects the properties and values ​​passed by the parent component

<template>
    <div>{
   
   { info }}</div>
</template>
 
<script setup>
import { ref, inject} from "vue"
const info = inject("info1", "默认值")
</script>

result:

 

おすすめ

転載: blog.csdn.net/limif/article/details/126461063