一、核心概念(面试必答)
provide 和 inject 是 Vue 中用于跨层级组件通信 的 API(Vue2/3 均支持),解决了 props 逐级透传(prop drilling)的问题:
provide:在父组件(或祖先组件)中提供数据 / 方法,供所有后代组件使用。inject:在后代组件中注入祖先组件提供的数据 / 方法,无视组件层级深度。
核心特点:
- 非响应式(Vue2) :默认情况下,Vue2 中
provide的数据不是响应式的(修改父组件数据,子组件不会更新); - 响应式(Vue3) :Vue3 中配合
ref/reactive使用,默认支持响应式; - 不限制层级:无论后代组件嵌套多深,都能直接获取祖先数据,无需逐级传递;
- 按需注入:后代组件可选择性注入需要的数据,未提供时可设置默认值。
二、代码示例(分 Vue2 和 Vue3)
<!-- 祖先组件(父/爷组件):提供数据 -->
<template>
<div>
<h2>祖先组件</h2>
<button @click="changeTheme">切换主题</button>
<child-component />
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue'
export default {
components: { ChildComponent },
// 提供数据/方法
provide() {
return {
theme: this.theme, // 非响应式(Vue2 需手动处理响应式)
changeTheme: this.changeTheme // 方法可传递,调用时绑定this
}
},
data() {
return {
theme: 'light' // 初始主题
}
},
methods: {
changeTheme() {
this.theme = this.theme === 'light' ? 'dark' : 'light'
// Vue2 中修改后,后代组件的 theme 不会自动更新,需额外处理(如用 Vue.observable)
}
}
}
</script>
<!-- 后代组件(孙组件,嵌套多层):注入数据 -->
<template>
<div>
<h3>孙组件 - 当前主题:{{ theme }}</h3>
<button @click="changeTheme">通过inject切换主题</button>
</div>
</template>
<script>
export default {
// 注入祖先提供的数据/方法
inject: ['theme', 'changeTheme'],
// 若担心未提供,可设置默认值
// inject: {
// theme: { default: 'light' },
// changeTheme: { default: () => {} }
// }
}
</script>
-
Vue3 示例(组合式 API)
<template></template> <script setup> import { ref, provide } from 'vue' import ChildComponent from './ChildComponent.vue'祖先组件 - 主题:{{ theme }}
<button @click="changeTheme">切换主题</button> <ChildComponent />// 响应式数据
const theme = ref('light')
// 提供方法
const changeTheme = () => {
theme.value = theme.value === 'light' ? 'dark' : 'light'
}// 提供数据(配合ref/reactive实现响应式)
<template>
provide('theme', theme)
provide('changeTheme', changeTheme)
</script></template> <script setup> import { inject } from 'vue'孙组件 - 主题:{{ theme }}
<button @click="changeTheme">切换主题</button>// 注入数据,设置默认值防止未提供
const theme = inject('theme', ref('light'))
const changeTheme = inject('changeTheme', () => {})
</script>
三、实际业务场景(面试加分项)
provide/inject 适用于跨多层级、非父子但有统一上下文的场景,以下是高频业务场景:
1. 全局主题 / 配置管理
- 场景:项目中需要全局切换主题(如暗黑模式)、统一配置(如接口前缀、全局语言),且多个层级的组件都需要使用这些配置。
- 优势 :无需在每个组件中通过
props传递,也无需引入 Vuex/Pinia(简单场景),直接在根组件provide全局配置,所有后代组件inject使用。
2. 权限控制
- 场景:项目中不同角色(管理员 / 普通用户)有不同的权限,根组件获取用户权限后,提供给所有后代组件(如按钮是否显示、菜单是否渲染)。
示例:
<!-- 根组件 -->
<script setup>
import { provide, ref } from 'vue'
const userRole = ref('admin') // 从接口获取
provide('userRole', userRole)
</script>
<!-- 深层子组件 -->
<script setup>
import { inject } from 'vue'
const userRole = inject('userRole')
const isAdmin = userRole.value === 'admin'
</script>
3. 组件库封装
- 场景 :开发通用组件库(如表单组件、弹窗组件)时,父组件(如
Form)需要给子组件(如FormItem)传递配置(如校验规则、布局方式),但FormItem可能嵌套在其他组件中,无法直接用props。 - 示例 :Element UI 的
ElForm/ElFormItem就是基于provide/inject实现的,ElForm提供校验方法,ElFormItem注入后调用校验。
4. 路由 / 状态透传(简单场景)
- 场景 :小型项目中,无需引入 Pinia/Vuex 时,可通过
provide在路由入口组件提供全局状态(如用户信息),所有页面组件直接inject使用。 - 注意 :大型项目仍推荐 Pinia/Vuex,
provide/inject不适合复杂状态管理。
5. 全局加载 / 提示
- 场景 :全局加载状态(如接口请求中)、全局提示方法(如
message),在根组件提供后,所有组件可直接调用,无需每次引入。
四、面试避坑点(高频追问)
-
响应式问题 :
- Vue2 中
provide的普通数据非响应式,需用Vue.observable包装:
provide() { return { theme: Vue.observable({ value: 'light' }) } }provide/inject是 Vue 跨层级组件通信的核心 API,解决props逐级透传问题,Vue3 配合ref/reactive支持响应式;- 核心业务场景:全局主题 / 配置、权限控制、组件库封装、简单全局状态管理;
- 面试注意:需掌握响应式处理、适用场景边界,以及与 Pinia/Vuex 的区别(
provide/inject适合轻量跨层级通信,Pinia 适合复杂全局状态管理)
- Vue2 中