一、基础

Provide + inject 主要用于父子组件之间的数据使用(孙组件用爷组件的数据)

  • 爷组件

    1. import { provide, ref } from '@vue/runtime-core'
    2. const str = ref('爷组件的数据')
    3. provide("str", str);
  • 父组件 ```typescript import { inject, Ref } from “vue”;

const str:Ref = inject(“str”);

  1. - 子组件
  2. ```typescript
  3. import { Ref } from "vue"
  4. import { inject } from "@vue/runtime-core";
  5. const str: Ref<string> = inject("str")
  • 结果展示

截屏2022-07-02 16.29.50.png