From f62f378f42644f87b9ef2fe151db937d3aa93175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Caan=20=28=E9=99=88=E6=A0=8B=29?= Date: Tue, 8 Jun 2021 14:28:19 +0800 Subject: [PATCH] chore: add util for install component (#707) --- src/components/CodeEditor/index.ts | 15 +++------------ src/components/FlowChart/index.ts | 8 ++------ src/utils/install.ts | 12 ++++++++++++ 3 files changed, 17 insertions(+), 18 deletions(-) create mode 100644 src/utils/install.ts diff --git a/src/components/CodeEditor/index.ts b/src/components/CodeEditor/index.ts index 89aa8a0e..faefea51 100644 --- a/src/components/CodeEditor/index.ts +++ b/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); diff --git a/src/components/FlowChart/index.ts b/src/components/FlowChart/index.ts index f4446a70..30688353 100644 --- a/src/components/FlowChart/index.ts +++ b/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); diff --git a/src/utils/install.ts b/src/utils/install.ts new file mode 100644 index 00000000..f2687b26 --- /dev/null +++ b/src/utils/install.ts @@ -0,0 +1,12 @@ +import { App, Plugin } from 'vue'; + +export const install = (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; +}; -- GitLab