Jelajahi Sumber

chore: add util for install component (#707)

Leo Caan (陈栋) 3 tahun lalu
induk
melakukan
f62f378f42

+ 3 - 12
src/components/CodeEditor/index.ts

@@ -1,15 +1,6 @@
-import type { App } from 'vue';
+import { install } from '/@/utils/install';
 import codeEditor from './src/CodeEditor.vue';
 import jsonPreview from './src/json-preview/JsonPreview.vue';
 
-export const CodeEditor = Object.assign(codeEditor, {
-  install(app: App) {
-    app.component(codeEditor.name, codeEditor);
-  },
-});
-
-export const JsonPreview = Object.assign(jsonPreview, {
-  install(app: App) {
-    app.component(jsonPreview.name, jsonPreview);
-  },
-});
+export const CodeEditor = install(codeEditor);
+export const JsonPreview = install(jsonPreview);

+ 2 - 6
src/components/FlowChart/index.ts

@@ -1,8 +1,4 @@
-import type { App } from 'vue';
+import { install } from '/@/utils/install';
 import flowChart from './src/FlowChart.vue';
 
-export const FlowChart = Object.assign(flowChart, {
-  install(app: App) {
-    app.component(flowChart.name, flowChart);
-  },
-});
+export const FlowChart = install(flowChart);

+ 12 - 0
src/utils/install.ts

@@ -0,0 +1,12 @@
+import { App, Plugin } from 'vue';
+
+export const install = <T>(component: T, alias?: string) => {
+  const C = component as any;
+  C.install = (app: App) => {
+    app.component(C.name, component);
+    if (alias) {
+      app.config.globalProperties[alias] = component;
+    }
+  };
+  return component as T & Plugin;
+};