【Vue3+Vite+TS】5.0 利用app.use实现全局组件注册

修改src\components\baseline\chooseIcon\index.ts

/*
 * @Author: bobokaka
 * @Date: 2021-12-22 21:58:24
 * @LastEditTime: 2021-12-22 23:58:54
 * @LastEditors: bobokaka
 * @Description: chooseIcon导出,用于全局注册
 * @FilePath: \vue3-element-ui-baseline\src\components\baseline\chooseIcon\src\index.ts
 */
import { App } from 'vue'
import ChooseIcon from './src/index.vue'

export { ChooseIcon }

//组件可通过use的形式使用
export default {
    ChooseIcon,
    install(app: App) {
        app.component('bs-choose-icon', ChooseIcon)
    },
}

修改src\components\baseline\chooseArea\src\index.vue

/*
 * @Author: bobokaka
 * @Date: 2021-12-21 19:47:32
 * @LastEditTime: 2021-12-22 21:55:49
 * @LastEditors: bobokaka
 * @Description: chooseArea导出,用于全局注册
 * @FilePath: \vue3-element-ui-baseline\src\components\baseline\chooseArea\index.ts
 */
import { App } from 'vue'
import ChooseArea from './src/index.vue'

export { ChooseArea }

//组件可通过use的形式使用
export default {
    ChooseArea,
    install(app: App) {
        app.component('bs-choose-area', ChooseArea)
    },
}

修改src\components\baseline\index.ts

/*
 * @Author: bobokaka
 * @Date: 2021-12-22 21:56:58
 * @LastEditTime: 2021-12-22 21:57:59
 * @LastEditors: bobokaka
 * @Description: Container导出,用于全局注册
 * @FilePath: \vue3-element-ui-baseline\src\components\baseline\container\index.ts
 */
import { App } from 'vue'
import Container from './src/index.vue'

export { Container }

//组件可通过use的形式使用
export default {
    Container,
    install(app: App) {
        app.component('bs-container', Container)
    },
}

修改

/*
 * @Author: bobokaka
 * @Date: 2021-12-20 01:41:54
 * @LastEditTime: 2021-12-22 22:02:44
 * @LastEditors: bobokaka
 * @Description: 全局基础组件全局注册
 * @FilePath: \vue3-element-ui-baseline\src\components\baseline\index.ts
 */
import { App } from 'vue'
import ChooseArea from './chooseArea'
import ChooseIcon from './chooseIcon'
import Container from './container'

const components = [ChooseArea, ChooseIcon, Container]
export { ChooseArea, ChooseIcon, Container }

//组件可通过use的形式使用
export default {
    ChooseArea,
    ChooseIcon,
    Container,
    install(app: App) {
        components.map(item => {
            app.use(item)
        })
    },
}

修改src\main.ts
增加

import BaselineUI from './components/baseline'
app..use(BaselineUI)

全部代码:

/*
 * @Author: bobokaka
 * @Date: 2021-12-19 10:57:16
 * @LastEditTime: 2021-12-22 22:33:24
 * @LastEditors: bobokaka
 * @Description: main.ts
 * @FilePath: \vue3-element-ui-baseline\src\main.ts
 */
import { createApp } from 'vue'
import App from './App.vue'
import router from './router/index'

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as Icons from '@element-plus/icons-vue'
import { toLine } from './utils'

import BaselineUI from './components/baseline'

import './style/index.scss'

const app = createApp(App)

//全局组件注册,牺牲一些性能,但方便使用
//封装成el-icon-xxx
for (let i in Icons) {
    // console.log(`i:`, i)
    // console.log('Icons[i]', (Icons as any)[i])
    app.component(`el-icon-${toLine(i)}`, (Icons as any)[i])
}

app.use(router).use(ElementPlus).use(BaselineUI)

app.mount('#app')

修改src\views\chooseIcon\index.vue






修改src\views\chooseArea\index.vue





最终效果和修改之前一样。

你可能感兴趣的:(【Vue3+Vite+TS】5.0 利用app.use实现全局组件注册)