vue3 vue.config.js配置Element-plus组件和Icon图标实现按需自动引入

这篇具有很好参考价值的文章主要介绍了vue3 vue.config.js配置Element-plus组件和Icon图标实现按需自动引入。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

打包时,报警告,提示包太大会影响性能

1.配置前包体积:

vue3 vue.config.js配置Element-plus组件和Icon图标实现按需自动引入

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自动按需引入element-plus,
            require('unplugin-element-plus/webpack')({
                libs: [{
                    libraryName: 'element-plus',
                    esModule: true,
                    resolveStyle: (name) => {
                        return `element-plus/theme-chalk/${name}.css`
                    },
                }, ]
            }),
            AutoImport({
                resolvers: [
                    // 自动导入图标组件
                    IconsResolver({
                        prefix: 'Icon',
                    }),
                    ElementPlusResolver()
                ]
            }),
            Components({
                resolvers: [
                    // 自动注册图标组件
                    IconsResolver({
                        enabledCollections: ['ep'],
                    }),
                    ElementPlusResolver()
                ]
            }),
            Icons({
                autoInstall: true,
            }),
        ],
    },

})

4.使用

在页面直接使用,直接使用 SVG 图标,当做一般的 svg 使用
icon使用时需要用以下两种方式方式:

<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里面,则会比放在button里大
<el-button type="primary" size="small" @click="searchResource"><i-ep-search/>查询</el-button>

6.按需引入后,ElMessageBox样式错乱

解决方法一:在当前页面或者全局main.js里面引入import "element-plus/es/components/message-box/style/css";即可,但是违背了按需引入的初衷

解决方法二按需导入: 使用unplugin-element-plus对使用到的组件样式进行按需导入 

npm i unplugin-element-plus -D

然后再vue.config.js中配置以下即可:

 plugins: [
            //配置webpack自动按需引入element-plus,
            require('unplugin-element-plus/webpack')({
                libs: [{
                    libraryName: 'element-plus',
                    esModule: true,
                    resolveStyle: (name) => {
                        return `element-plus/theme-chalk/${name}.css`
                    },
                }, ]
            }),
....
]

7.使用按需导入后,使用配置文件自动化生成表单中,配置得icon:'Edit'失效

全局引入时,直接使用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
        }]

注意:使用组件时,必须使用shallowRef或者 markRaw对组件进行包装,否则会报警告

[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: ƒ} 

报错原因:vue接收到一个组件,该组件是一个响应式对象。这可能会导致不必要的性能开销,应该通过使用’markRaw‘或使用’shallowRef‘而不是’ref'来避免。
写成:shallowRef(Edit)或者markRaw(Edit)即可 

8.其他打包警告

警告:
chunk 574 [mini-css-extract-plugin]
Conflicting order. Following module has been added:

vue3 vue.config.js配置Element-plus组件和Icon图标实现按需自动引入 

解决:由于各个css和js文件引入顺序问题导致

module.exports = defineConfig({
......
    css: {
        extract: {
            ignoreOrder: true
        }
    }
})

9.配置后包体积大小

vue3 vue.config.js配置Element-plus组件和Icon图标实现按需自动引入

 文章来源地址https://www.toymoban.com/news/detail-462386.html

到了这里,关于vue3 vue.config.js配置Element-plus组件和Icon图标实现按需自动引入的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue3+element-plus组件下拉列表,数组数据转成树形数据

    引入组件 可以直接在项目中引入element-plus表格组件,如果需要变成下拉列表样式需要添加以下属性: row-key 必填 最好给数字或唯一属性 , 给每个节点设置id 不填的话 没有办法实现展开效果 load 这个是动态添加数据的 前提(开启lazy ,表格数组里设置了hasChildren:true) tre

    2024年02月13日
    浏览(34)
  • vue3项目搭建并配置element-plus

    安装完成后,输入如下指令查看vue的版本: 选择一个要存放项目的目录,打开小黑窗输入如下命令: 一开始输入项目名称或者默认vue-project,然后根据需求选择Yes/No 生成完项目后,输入如下指令: src/main.js里引入 index.css的文件位置根据实际情况写,也有可能是 const app后面加

    2024年02月13日
    浏览(38)
  • vue-treeselect(适配Vue3.2)及Element-plus的TreeSelect组件

    1.1、vue2 使用的版本 官网地址:https://www.npmjs.com/package/@riophae/vue-treeselect 是3年前更新的 安装: 如果你的项目是vue3的话,使用该安装命令会提示,此命令只针对vue2.2版本 具体提示内容是: peerDependencies WARNING @riophae/vue-treeselect@latest requires a peer of vue@^2.2.0 but vue@3.2.39 was instal

    2024年02月15日
    浏览(46)
  • Vue3+Element-Plus中的Tree组件,多选时的赋值与取值

    el-tree 是 element-ui 提供的一个树形组件,可以在 Vue.js 应用中使用。el-tree 组件提供了 getCheckedNodes 方法,可以用来获取树形结构中被选中的节点。

    2024年02月14日
    浏览(33)
  • 【Vue3 博物馆管理系统】使用Vue3、Element-plus菜单组件构建前台用户菜单

    第一章 定制上中下(顶部菜单、底部区域、中间主区域显示)三层结构首页 第二章 使用Vue3、Element-plus菜单组件构建菜单 [第三章 使用Vue3、Element-plus菜单组件构建轮播图] [第四章 使用Vue3、Element-plus菜单组件构建组图文章] 上一章节给我们把博物馆管理系统打了个地基,基本

    2024年02月13日
    浏览(45)
  • vue3使用element-plus 树组件(el-tree)数据回显

    html搭建结构 js 非常好用,拿过来修改一下check事件,ref获取就直接可以使用了 

    2024年04月09日
    浏览(32)
  • Vue3自动引入组件(unplugin-auto-import和element-plus)

    在使用 Vue3 开发项目时,我们经常需要引入多个组件,但是每次手动引入非常麻烦,容易出错。为了解决这个问题,我们可以使用 unplugin-auto-import 插件自动引入组件,提高开发效率。本篇博客将详细介绍如何在 Vue3 项目中使用 unplugin-auto-import 插件。 首先,在项目中安装 un

    2024年02月13日
    浏览(36)
  • [Vue3 博物馆管理系统] 使用Vue3、Element-plus tabs组件构建选项卡功能

    第一章 定制上中下(顶部菜单、底部区域、中间主区域显示)三层结构首页 第二章 使用Vue3、Element-plus菜单组件构建菜单 第三章 使用Vue3、Element-plus走马灯组件构建轮播图 第四章 使用Vue3、Element-plus tabs组件构建选项卡功能 [第五章 使用Vue3、Element-plus菜单组件构建组图文章

    2024年02月09日
    浏览(31)
  • 沉淀自己的pro-table组件,并发布到npm(Vue3、element-plus)

    传送门 约定:npm包名 vue3-el-pro-table ,引用 vue3-el-pro-table 的包名为“本项目”。 声明: Vue3ProTable.vue 代码是在这个项目的基础上进行修改的。 作者:hans774882968以及hans774882968以及hans774882968 Quick Start src/main.ts Then use vue3-pro-table / directly in .vue file. Import interface: Component props defi

    2024年02月16日
    浏览(39)
  • vue3+ts+element-plus 之使用node.js对接mysql进行表格数据展示

    * 初始化node 查看node是否安装 node -v 初始化命令 npm init 初始化配置解释如下: 完成后会有一个package.json文件 * 安装可能用到的依赖 根据需求安装,我这里需要对接mysql,安装依赖 ,我是一次性安装完,后边会直接使用,也可以边安装边使用。如下 安装成功如下: * 配置文件

    2024年02月15日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包