Showing
2 changed files
with
25 additions
and
0 deletions
src/hooks/injectionSymbols.js
0 → 100644
src/hooks/useContext.js
0 → 100644
| 1 | +import { provide, inject } from "vue"; | ||
| 2 | + | ||
| 3 | +// const key = Symbol(); | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * 创建全局变量 | ||
| 7 | + * @param {*} context | ||
| 8 | + * @param {*} key | ||
| 9 | + */ | ||
| 10 | +export function createContext(context, key) { | ||
| 11 | + provide(key, context) | ||
| 12 | +} | ||
| 13 | +/** | ||
| 14 | + * 使用全局变量 | ||
| 15 | + * @param {*} key | ||
| 16 | + * @returns | ||
| 17 | + */ | ||
| 18 | +export function useContext(key) { | ||
| 19 | + return inject(key) | ||
| 20 | +} |
-
Please register or login to post a comment