Skip to content

Commit 122dc09

Browse files
committed
添加useContext方法
1 parent 6c504ad commit 122dc09

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
node_modules
22
.DS_Store
3-
dist/
3+
dist
44
dist-ssr
55
*.local

src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ export default function(app: App, Vue: Context["Vue"]) {
1919
}
2020
export { useModule } from "./use-module";
2121
export { uninstall, clear, listUnistaller } from "./uninstaller";
22+
export { useContext } from "./use-context";

src/use-context.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* 使用上下文对象里的数据
3+
* @param key 上下文对象中的key
4+
* @returns 根据key获取的上下文对象中的值
5+
*/
6+
function useContext(key: string): any {
7+
if (!window[Symbol.for("___VML_CONTEXT___")])
8+
throw new Error(
9+
"[vue-module-loader]: 请先使用app.use方法安装vue-module-loader插件或使用useModule方法加载一个模块。"
10+
);
11+
const target = window[Symbol.for("___VML_CONTEXT___")][key];
12+
if (target === undefined)
13+
throw new Error(`[vue-module-loader]:上下文中不存在“${key}”`);
14+
return target;
15+
}
16+
export { useContext };

0 commit comments

Comments
 (0)