Skip to content

Commit

Permalink
perf(插件): 插件更新为自动化加载
Browse files Browse the repository at this point in the history
  • Loading branch information
CNMathon committed Sep 18, 2020
1 parent 89b0a19 commit e0d39c4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
16 changes: 8 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { AppConfig } from './config/app'
import App from '@/App.vue'
import router from '@/router'
import store from '@/store'
import { AppConfig } from '@/config/app'

import { loadAtdComponent } from '@/plugins/antd'
import { registeGlobalComponent } from './components/index'
import { loadAllPlugins } from '@/plugins'
import { registeGlobalComponent } from '@/components/index'

/** 导入第三方插件 */
import '@/plugins'
Expand All @@ -14,8 +14,8 @@ import '@/plugins'
const app: ReturnType<typeof createApp> = createApp(App)
app.config.globalProperties = AppConfig

/** 手动注册 antd-vue 组件 */
loadAtdComponent(app)
/** 加载所有 Plugins */
loadAllPlugins(app)

/** 自动注册全局组件 */
registeGlobalComponent(app)
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/antd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { createApp } from 'vue'
/**
* @description 手动注册 antd-vue 组件,达到按需加载目的
* @description Automatically register components under Button, such as Button.Group
* @param {ReturnType<typeof createApp>} app 整个应用的实例
* @param {ReturnType<typeof createApp>} app 整个应用的实例
* @returns void
*/
export function loadAtdComponent(app: ReturnType<typeof createApp>) {
export default function loadComponent(app: ReturnType<typeof createApp>) {
app.use(Button)
app.use(Card)
app.use(Row)
Expand Down
13 changes: 12 additions & 1 deletion src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
/** 在此处统一导出插件,保持入口文件简介 */
import { createApp } from 'vue'

/**
* @description 加载所有 Plugins
* @param {ReturnType<typeofcreateApp>} app 整个应用的实例
*/
export function loadAllPlugins(app: ReturnType<typeof createApp>) {
const files = require.context('.', true, /\.ts$/)
files.keys().forEach((key) => {
if (key !== './index.ts') files(key).default(app)
})
}

0 comments on commit e0d39c4

Please sign in to comment.