inject 和provide 的用法之实现监听sessionStorage的变化做对应操作

一、inject 和provide 的用法

inject 和 provide 是 Vue 3 中的依赖注入机制,用于在组件树中传递数据,而不需要通过 props 逐级传递

1. 基本用法
复制代码
// 父组件
import { provide } from 'vue'

const message = ref('Hello')
provide('key', message)

// 子组件
import { inject } from 'vue'

const message = inject('key')
2. 使用 Symbol 作为 key(推荐)
复制代码
// types.ts
export const MESSAGE_KEY = Symbol()

// 父组件
import { MESSAGE_KEY } from './types'
provide(MESSAGE_KEY, message)

// 子组件
import { MESSAGE_KEY } from './types'
const message = inject(MESSAGE_KEY)
3. 提供默认值
复制代码
const message = inject('key', '默认值')
4. 类型声明:
复制代码
interface MessageContext {
  message: Ref<string>
  updateMessage: (value: string) => void
}

// 父组件
provide<MessageContext>('messageContext', {
  message,
  updateMessage: (value) => { message.value = value }
})

// 子组件
const { message, updateMessage } = inject<MessageContext>('messageContext')!
5. 只读数据
复制代码
import { readonly } from 'vue'

provide('count', readonly(count))

二、注意事项

  • provide 和 inject 绑定不是响应式的。但如果你传递了一个响应式对象,那么它的属性仍然是响应式的。
  • 尽量在 setup() 函数中使用 provide 和 inject。
  • 不要在子组件中修改注入的值,除非父组件明确允许。
  • 使用 Symbol 作为 key 可以避免命名冲突。
  • 考虑使用 TypeScript 来增强类型安全。
  • 使用 provide 和 inject 可以简化组件间的数据传递,特别是对于深层嵌套的组件结构。但也要注意不要过度使用,以免使数据流变得难以追踪。

三、案例代码

要实现点击 tab 获取最新的 sessionStorage.getItem('activeTabType'),可以按以下步骤修改代码:

  1. 修改 getActiveTabType 计算属性

    const getActiveTabType = computed(() => {
    return sessionStorage.getItem('activeTabType') || '';
    });

  2. 在 AggregateAnalysisTab(子组件) 组件中,当 tab 被点击时,更新 sessionStorage:

    // 在 AggregateAnalysisTab.vue 中
    const handleTabClick = (tabType: string) => {
    sessionStorage.setItem('activeTabType', tabType);
    };

  3. 在父组件中添加一个响应式变量来触发重新计算:

    const tabUpdateTrigger = ref(0);

    const getActiveTabType = computed(() => {
    tabUpdateTrigger.value; // 添加这行来依赖这个变量
    return sessionStorage.getItem('activeTabType') || '';
    });

  4. 使用 provide/inject 或事件总线在 AggregateAnalysisTab 中通知父组件更新:

    // 在父组件中
    provide('updateTabType', () => {
    tabUpdateTrigger.value++;
    });

    // 在 AggregateAnalysisTab.vue 中
    const updateTabType = inject('updateTabType');

    const handleTabClick = (tabType: string) => {
    sessionStorage.setItem('activeTabType', tabType);
    updateTabType();
    };

这样,每次点击 tab 时,sessionStorage 会被更新,并且父组件的 getActiveTabType 计算属性会重新计算,获取最新的值。

四、类型问题

如: "updateTabType"的类型为"未知"

  1. 在父组件中定义 updateTabType 的类型:

    type UpdateTabType = () => void;

    // 在父组件中
    const updateTabType: UpdateTabType = () => {
    tabUpdateTrigger.value++;
    };

    provide('updateTabType', updateTabType);

  2. 在 子组件 AggregateAnalysisTab.vue 中,我们可以这样使用 inject:

    import { inject } from 'vue';

    type UpdateTabType = () => void;

    const updateTabType = inject('updateTabType') as UpdateTabType;

相关推荐
aichitang202412 分钟前
HTML 实时预览工具
前端·html
广州智维科技12 分钟前
Kvaser Edge WL400S:工业级边缘计算与 CAN‑FD 数据采集平台解析73-30130-01688-0
前端·edge·边缘计算
吃好睡好便好30 分钟前
在Matlab中用sphere( )函数绘制球面图
开发语言·前端·javascript·学习·算法·matlab·信息可视化
黑贝是条狗32 分钟前
注册表破解chrome,edge阻止浏览器连接本地websocket
前端·javascript·数据库
UXbot34 分钟前
AI 原型工具对比(2026):从文字描述到完整 App 界面的 5 款主流平台评测
android·前端·ios·交互·软件构建
ZC跨境爬虫37 分钟前
跟着 MDN 学 HTML day_53:(深入理解 XPathResult 接口)
前端·javascript·ui·html·音视频
摸鱼仙人~40 分钟前
html-anything 仓库全面介绍
前端·html
之歆43 分钟前
DAY_24JavaScript 面向对象深度全解:Object、构造函数与 this 系统指南(上)
开发语言·前端·javascript·原型模式
梦梦代码精1 小时前
开源智能体平台 BuildingAI 深度解析:Monorepo 架构、MCP 集成及 GPT-Image-2 接入实测
前端·人工智能·后端·gpt·开源·github
Asurplus1 小时前
【VUE】17、使用JSEncrypt对数据加解密
javascript·vue.js·jsencrypt·rsa