vue3中vue.config.js配置Element-plus組件和Icon圖標(biāo)實(shí)現(xiàn)按需自動(dòng)引入實(shí)例代碼
打包時(shí),報(bào)警告,提示包太大會(huì)影響性能
1.配置前包體積:

2.安裝插件:
npm i unplugin-auto-import unplugin-vue-components unplugin-icons -D
3.vue.config.js中加入以下配置:
const { defineConfig } = require('@vue/cli-service')
const AutoImport = require('unplugin-auto-import/webpack')
const Components = require('unplugin-vue-components/webpack')
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')
const Icons = require('unplugin-icons/webpack')
const IconsResolver = require('unplugin-icons/resolver')
module.exports = defineConfig({
...
configureWebpack: {
plugins: [
//配置webpack自動(dòng)按需引入element-plus,
require('unplugin-element-plus/webpack')({
libs: [{
libraryName: 'element-plus',
esModule: true,
resolveStyle: (name) => {
return `element-plus/theme-chalk/${name}.css`
},
}, ]
}),
AutoImport({
resolvers: [
// 自動(dòng)導(dǎo)入圖標(biāo)組件
IconsResolver({
prefix: 'Icon',
}),
ElementPlusResolver()
]
}),
Components({
resolvers: [
// 自動(dòng)注冊(cè)圖標(biāo)組件
IconsResolver({
enabledCollections: ['ep'],
}),
ElementPlusResolver()
]
}),
Icons({
autoInstall: true,
}),
],
},
})4.使用
在頁面直接使用,直接使用 SVG 圖標(biāo),當(dāng)做一般的 svg 使用
icon使用時(shí)需要用以下兩種方式方式:
<el-icon>
<i-ep-edit/>
</el-icon><i-ep-edit/>
5.在el-button中使用
如果用在el-button里面的icon屬性上使用,用SVG方式無效,還是需要引入再使用(不知道有沒有其他方式)
import { Delete, Edit } from '@element-plus/icons-vue'
<el-button type="success" size="small" :icon="Edit" round @click="openAddUserForm('add')">新增用戶</el-button>注意:
使用:icon="Edit"則icon的大小和button里面字體大小一致size=small
但是如果使用<i-ep-search/>放在el-button里面,則會(huì)比放在button里大
<el-button type="primary" size="small" @click="searchResource"><i-ep-search/>查詢</el-button>
6.按需引入后,ElMessageBox樣式錯(cuò)亂
解決方法一:在當(dāng)前頁面或者全局main.js里面引入import "element-plus/es/components/message-box/style/css";即可,但是違背了按需引入的初衷
解決方法二按需導(dǎo)入: 使用unplugin-element-plus對(duì)使用到的組件樣式進(jìn)行按需導(dǎo)入
npm i unplugin-element-plus -D
然后再vue.config.js中配置以下即可:
plugins: [
//配置webpack自動(dòng)按需引入element-plus,
require('unplugin-element-plus/webpack')({
libs: [{
libraryName: 'element-plus',
esModule: true,
resolveStyle: (name) => {
return `element-plus/theme-chalk/${name}.css`
},
}, ]
}),
....
]7.使用按需導(dǎo)入后,使用配置文件自動(dòng)化生成表單中,配置得icon:'Edit'失效
全局引入時(shí),直接使用icon: 'Edit',然后jsx中直接讀取即可
buttons: [{
name: '生成案例',
title: 'generateTestCase',
type: 'primary',
size: 'default', //可以是default,small,large
icon: 'Edit',
// 按鈕是否為樸素類型
// plain: true,
onClick: null
}
] const Button = (form, data) =>(
!data.isHidden?<ElButton
type={data.type}
size={data.size}
icon= {data.icon}
plain={data.plain}
onClick={data.onClick}
>
{data.name}</ElButton>:''
)但是按需引入后,這樣做失效了。
解決:直接把icon: shallowRef(Edit)或者markRaw(Edit),然后引入組件即可
import { DocumentDelete, Edit, Download } from '@element-plus/icons-vue'
import { shallowRef } from 'vue'
buttons: [{
name: '生成案例',
title: 'generateTestCase',
type: 'primary',
size: 'default', //可以是default,small,large
icon: shallowRef(Edit),
// 按鈕是否為樸素類型
// plain: true,
onClick: null
}]注意:使用組件時(shí),必須使用shallowRef或者 markRaw對(duì)組件進(jìn)行包裝,否則會(huì)報(bào)警告
[Vue warn]: Vue received a Component which was made a reactive object. This can lead to unnecessary performance overhead, and should be avoided by marking the component with `markRaw` or using `shallowRef` instead of `ref`.
Component that was made reactive: {name: "DocumentDelete", __file: "document-delete.vue", render: ƒ}
報(bào)錯(cuò)原因:vue接收到一個(gè)組件,該組件是一個(gè)響應(yīng)式對(duì)象。這可能會(huì)導(dǎo)致不必要的性能開銷,應(yīng)該通過使用’markRaw‘或使用’shallowRef‘而不是’ref'來避免。
寫成:shallowRef(Edit)或者markRaw(Edit)即可
8.其他打包警告
警告:
chunk 574 [mini-css-extract-plugin]
Conflicting order. Following module has been added:

解決:由于各個(gè)css和js文件引入順序問題導(dǎo)致
module.exports = defineConfig({
......
css: {
extract: {
ignoreOrder: true
}
}
})9.配置后包體積大小

總結(jié)
到此這篇關(guān)于vue3中vue.config.js配置Element-plus組件和Icon圖標(biāo)實(shí)現(xiàn)按需自動(dòng)引入的文章就介紹到這了,更多相關(guān)vue3 vue.config.js按需自動(dòng)引入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3+vite使用vite-plugin-svg-icons插件顯示本地svg圖標(biāo)的方法
這篇文章主要介紹了vue3+vite使用vite-plugin-svg-icons插件顯示本地svg圖標(biāo)的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12
Vue實(shí)現(xiàn)簡單選項(xiàng)卡功能
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)簡單選項(xiàng)卡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
如何在ElementUI的上傳組件el-upload中設(shè)置header
這篇文章主要介紹了如何在ElementUI的上傳組件el-upload中設(shè)置header,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
解決vue prop傳值default屬性如何使用,為何不生效的問題
這篇文章主要介紹了解決vue prop傳值default屬性如何使用,為何不生效的問題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Uniapp 實(shí)現(xiàn)頂部標(biāo)簽頁切換功能(詳細(xì)步驟)
本文介紹了如何在UniApp中實(shí)現(xiàn)頂部標(biāo)簽頁切換功能,u-tab-bar組件提供了便捷的標(biāo)簽切換功能和豐富的樣式選項(xiàng),而swiper組件則更加靈活,支持自定義切換方式,根據(jù)自己的需求選擇合適的方式實(shí)現(xiàn)頂部標(biāo)簽頁切換,感興趣的朋友一起看看吧2025-02-02
深入理解 Vue 3 的 watch及用法示例小結(jié)
watch是Vue3中處理響應(yīng)式數(shù)據(jù)變化邏輯的基石,這篇文章將帶你深入理解Vue3中的watch,涵蓋基礎(chǔ)用法、高級(jí)配置以及與watchEffect的對(duì)比,感興趣的朋友跟隨小編一起看看吧2026-02-02
vue動(dòng)態(tài)繪制四分之三圓環(huán)圖效果
這篇文章主要介紹了vue動(dòng)態(tài)繪制四分之三圓環(huán)圖效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
vue項(xiàng)目使用cdn加速減少webpack打包體積
通過壓縮代碼的手段可減小網(wǎng)絡(luò)傳輸?shù)拇笮?但實(shí)際上最影響用戶體驗(yàn)的還是網(wǎng)頁首次打開時(shí)的加載等待,其根本原因是網(wǎng)絡(luò)傳輸過程耗時(shí)較大,這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目使用cdn加速減少webpack打包體積的相關(guān)資料,需要的朋友可以參考下2022-08-08

