vue3集成Element-plus實(shí)現(xiàn)按需自動(dòng)引入組件的方法總結(jié)
- element-plus正是element-ui針對(duì)于vue3開發(fā)的一個(gè)UI組件庫,
- 它的使用方式和很多其他的組件庫是一樣的,其他類似于ant-design-vue、NaiveUI、VantUI都是差不多的;安裝element-plus
首先下載element-plus
npm install element-plus
1、第一種方式,使用全局引入
引入element-plus的方式是全局引入,代表的含義是所有的組件和插件都會(huì)被自動(dòng)注冊(cè),
優(yōu)點(diǎn):上手快
缺點(diǎn):會(huì)增大包的體積
在main.ts文件中
import { createApp } from 'vue'
// 全局引入
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'
import store from './store'
const app = createApp(App)
app.use(router)
app.use(store)
app.use(ElementPlus)
app.mount('#app')2、第二種方式,使用局部引入
局部引入也就是在開發(fā)中用到某個(gè)組件對(duì)某個(gè)組件進(jìn)行引入,
<template>
<div class="app">
<el-button>Default</el-button>
<el-button type="primary">Primary</el-button>
<el-button type="success">Success</el-button>
<el-button type="info">Info</el-button>
<el-button type="warning">Warning</el-button>
<el-button type="danger">Danger</el-button>
<el-button>中文</el-button>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
// 局部引入
import { ElButton } from 'element-plus'
import 'element-plus/theme-chalk/el-button.css'
import 'element-plus/theme-chalk/base.css'
export default defineComponent({
components: { ElButton },
setup() {
return {}
}
})
</script>
<style lang="less"></style>但是這樣我們?cè)陂_發(fā)時(shí)每次使用都要手動(dòng)在組件中引入對(duì)應(yīng)的css樣式,使用起來會(huì)比較麻煩
3、按需自動(dòng)引入element-plus 推薦
需要安裝unplugin-vue-components 和 unplugin-auto-import這兩款插件
npm install -D unplugin-vue-components unplugin-auto-import
安裝完成之后在vue.config.js文件中配置
// vue.config.js
const AutoImport = require('unplugin-auto-import/webpack')
const Components = require('unplugin-vue-components/webpack')
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')
module.exports = {
outputDir: './build',
// 和webpapck屬性完全一致,最后會(huì)進(jìn)行合并
configureWebpack: {
resolve: {
alias: {
components: '@/components'
}
},
//配置webpack自動(dòng)按需引入element-plus,
plugins: [
AutoImport({
resolvers: [ElementPlusResolver()]
}),
Components({
resolvers: [ElementPlusResolver()]
})
]
}
}按需自動(dòng)引入配置完之后,在組件中可直接使用,不需要引用和注冊(cè) 這里已經(jīng)實(shí)現(xiàn)了按需自動(dòng)移入Element-plus組件 組件中直接使用:
<template>
<div class="app">
<el-button>Default</el-button>
<el-button type="primary">Primary</el-button>
<el-button type="success">Success</el-button>
<el-button type="info">Info</el-button>
<el-button type="warning">Warning</el-button>
<el-button type="danger">Danger</el-button>
<el-button>中文</el-button>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
setup() {
return {}
}
})
</script>
<style lang="less"></style>效果:

總結(jié)
到此這篇關(guān)于vue3集成Element-plus實(shí)現(xiàn)按需自動(dòng)引入組件的文章就介紹到這了,更多相關(guān)vue3按需自動(dòng)引入組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3中實(shí)現(xiàn)拖拽排序代碼示例(vue-draggable-next的使用)
在Vue3中使用拖拽功能時(shí)應(yīng)選用vue-draggable-next插件,傳統(tǒng)的draggable插件不兼容Vue3,可能導(dǎo)致TypeError錯(cuò)誤,安裝后,需在項(xiàng)目中引入并使用,具體步驟包括安裝插件、引入使用、查看效果和相關(guān)說明,需要的朋友可以參考下2024-09-09
vue實(shí)現(xiàn)子路由調(diào)用父路由的方法
這篇文章主要介紹了vue實(shí)現(xiàn)子路由調(diào)用父路由的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
在Vue3項(xiàng)目中使用Vuex進(jìn)行狀態(tài)管理的詳細(xì)教程
在?Vue?3?中使用?Vuex?進(jìn)行狀態(tài)管理是一個(gè)很好的實(shí)踐,特別是在涉及到多個(gè)組件間共享狀態(tài)的情況,下面是如何在?Vue?3?項(xiàng)目中設(shè)置和使用?Vuex?的教程,包括?state,?mutations,?actions,?getters?的概念及其用途,需要的朋友可以參考下2024-09-09
vue使用keep-alive進(jìn)行組件緩存方法詳解(組件不緩存問題解決)
keep-alive包裹動(dòng)態(tài)組件時(shí),會(huì)緩存不活動(dòng)的組件實(shí)例,而不是銷毀它們,下面這篇文章主要給大家介紹了關(guān)于vue使用keep-alive進(jìn)行組件緩存方法(組件不緩存問題解決)的相關(guān)資料,需要的朋友可以參考下2022-09-09
vue視頻播放插件vue-video-player的具體使用方法
這篇文章主要介紹了vue視頻播放插件vue-video-player的具體使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

