浏览代码

feat: composable useInjection

nian 1 月之前
父节点
当前提交
28189026e6
共有 1 个文件被更改,包括 11 次插入0 次删除
  1. 11 0
      src/composable/useInjection.ts

+ 11 - 0
src/composable/useInjection.ts

@@ -0,0 +1,11 @@
+import { inject } from 'vue'
+
+import type { InjectionKey } from 'vue'
+
+export function useInjection<T>(key: InjectionKey<T> | string) {
+  const context = inject(key, null)
+  if (!context) {
+    throw new Error('useInjection must be used within Injection provider')
+  }
+  return context
+}