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)按鈕權(quán)限管理的項(xiàng)目實(shí)踐
在做后臺(tái)管理系統(tǒng)時(shí),經(jīng)常會(huì)有權(quán)限管理的功能,本文主要介紹了vue3實(shí)現(xiàn)按鈕權(quán)限管理的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08ant-design-vue 實(shí)現(xiàn)表格內(nèi)部字段驗(yàn)證功能
這篇文章主要介紹了ant-design-vue 實(shí)現(xiàn)表格內(nèi)部字段驗(yàn)證功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12vue跳轉(zhuǎn)同一路由報(bào)錯(cuò)的問題及解決
這篇文章主要介紹了vue跳轉(zhuǎn)同一路由報(bào)錯(cuò)的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Vue中使用localStorage存儲(chǔ)token并設(shè)置時(shí)效
這篇文章主要為大家介紹了Vue中使用localStorage存儲(chǔ)token并設(shè)置時(shí)效,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06使用vue-cli創(chuàng)建vue2項(xiàng)目的實(shí)戰(zhàn)步驟詳解
相信大部分Vue開發(fā)者都使用過vue-cli來構(gòu)建項(xiàng)目,它的確很方便,但對(duì)于很多初級(jí)開發(fā)者來說,還是要踩不少坑的,下面這篇文章主要給大家介紹了關(guān)于使用vue-cli創(chuàng)建vue2項(xiàng)目的實(shí)戰(zhàn)步驟,需要的朋友可以參考下2023-01-01