vue3 injection reports error injection "xxx" not found.

When encapsulating the CheckboxGroup component, you need to pass provide. The code is as follows:

//父组件
<template>
  <div class="envCheckBoxGroup">
    <slot></slot>
  </div>
</template>
<script setup>
import { provide } from "vue"
import { getCurrentInstance } from "vue"
const { proxy } = getCurrentInstance()
provide('CheckboxGroup', proxy)

</script>

Subassembly:

//子组件
const groups = inject('CheckboxGroup') 

But when the CheckboxGroup component is not used, a warning will be reported:

The best way to fix this problem is to add a second parameter when injecting

const groups = inject('CheckboxGroup',null) 

 The official website introduction is as follows:

https://cn.vuejs.org/api/composition-api-dependency-injection.html#inject

Guess you like

Origin blog.csdn.net/melissaomy/article/details/132368296