Commit f62f378f42644f87b9ef2fe151db937d3aa93175

Authored by Leo Caan (้™ˆๆ ‹)
Committed by GitHub
1 parent 6d5f9aa6

chore: add util for install component (#707)

src/components/CodeEditor/index.ts
1 -import type { App } from 'vue'; 1 +import { install } from '/@/utils/install';
2 import codeEditor from './src/CodeEditor.vue'; 2 import codeEditor from './src/CodeEditor.vue';
3 import jsonPreview from './src/json-preview/JsonPreview.vue'; 3 import jsonPreview from './src/json-preview/JsonPreview.vue';
4 4
5 -export const CodeEditor = Object.assign(codeEditor, {  
6 - install(app: App) {  
7 - app.component(codeEditor.name, codeEditor);  
8 - },  
9 -});  
10 -  
11 -export const JsonPreview = Object.assign(jsonPreview, {  
12 - install(app: App) {  
13 - app.component(jsonPreview.name, jsonPreview);  
14 - },  
15 -}); 5 +export const CodeEditor = install(codeEditor);
  6 +export const JsonPreview = install(jsonPreview);
src/components/FlowChart/index.ts
1 -import type { App } from 'vue'; 1 +import { install } from '/@/utils/install';
2 import flowChart from './src/FlowChart.vue'; 2 import flowChart from './src/FlowChart.vue';
3 3
4 -export const FlowChart = Object.assign(flowChart, {  
5 - install(app: App) {  
6 - app.component(flowChart.name, flowChart);  
7 - },  
8 -}); 4 +export const FlowChart = install(flowChart);
src/utils/install.ts 0 โ†’ 100644
  1 +import { App, Plugin } from 'vue';
  2 +
  3 +export const install = <T>(component: T, alias?: string) => {
  4 + const C = component as any;
  5 + C.install = (app: App) => {
  6 + app.component(C.name, component);
  7 + if (alias) {
  8 + app.config.globalProperties[alias] = component;
  9 + }
  10 + };
  11 + return component as T & Plugin;
  12 +};