vue3集成Element-plus實現(xiàn)按需自動引入組件的方法總結(jié)
- element-plus正是element-ui針對于vue3開發(fā)的一個UI組件庫,
- 它的使用方式和很多其他的組件庫是一樣的,其他類似于ant-design-vue、NaiveUI、VantUI都是差不多的;安裝element-plus
首先下載element-plus
npm install element-plus
1、第一種方式,使用全局引入
引入element-plus的方式是全局引入,代表的含義是所有的組件和插件都會被自動注冊,
優(yōu)點:上手快
缺點:會增大包的體積
在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ā)中用到某個組件對某個組件進(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>但是這樣我們在開發(fā)時每次使用都要手動在組件中引入對應(yīng)的css樣式,使用起來會比較麻煩
3、按需自動引入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屬性完全一致,最后會進(jìn)行合并
configureWebpack: {
resolve: {
alias: {
components: '@/components'
}
},
//配置webpack自動按需引入element-plus,
plugins: [
AutoImport({
resolvers: [ElementPlusResolver()]
}),
Components({
resolvers: [ElementPlusResolver()]
})
]
}
}按需自動引入配置完之后,在組件中可直接使用,不需要引用和注冊 這里已經(jīng)實現(xiàn)了按需自動移入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實現(xiàn)按需自動引入組件的文章就介紹到這了,更多相關(guān)vue3按需自動引入組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3中實現(xiàn)拖拽排序代碼示例(vue-draggable-next的使用)
在Vue3中使用拖拽功能時應(yīng)選用vue-draggable-next插件,傳統(tǒng)的draggable插件不兼容Vue3,可能導(dǎo)致TypeError錯誤,安裝后,需在項目中引入并使用,具體步驟包括安裝插件、引入使用、查看效果和相關(guān)說明,需要的朋友可以參考下2024-09-09
在Vue3項目中使用Vuex進(jìn)行狀態(tài)管理的詳細(xì)教程
在?Vue?3?中使用?Vuex?進(jìn)行狀態(tài)管理是一個很好的實踐,特別是在涉及到多個組件間共享狀態(tài)的情況,下面是如何在?Vue?3?項目中設(shè)置和使用?Vuex?的教程,包括?state,?mutations,?actions,?getters?的概念及其用途,需要的朋友可以參考下2024-09-09
vue使用keep-alive進(jìn)行組件緩存方法詳解(組件不緩存問題解決)
keep-alive包裹動態(tài)組件時,會緩存不活動的組件實例,而不是銷毀它們,下面這篇文章主要給大家介紹了關(guān)于vue使用keep-alive進(jìn)行組件緩存方法(組件不緩存問題解決)的相關(guān)資料,需要的朋友可以參考下2022-09-09
vue視頻播放插件vue-video-player的具體使用方法
這篇文章主要介紹了vue視頻播放插件vue-video-player的具體使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

