element-plus中如何实现按需导入与全局导入
作者:哈哈与黑洞大笑 发布时间:2024-05-02 17:04:21
标签:element-plus,按需导入,全局导入
按需导入:
安装插件
首先需要引入额外的插件:前**vite-plugin-components
已重命名为unplugin-vue-components
**
npm install unplugin-vue-components
配置插件
在weapack或vite配置文件内中添加配置
// vite.config.ts
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
?
export default {
?plugins: [
? ?// ...
? ?Components({
? ? ?resolvers: [ElementPlusResolver()],
? }),
],
}
// webpack.config.js
const Components = require('unplugin-vue-components/webpack')
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')
?
module.exports = {
?// ...
?plugins: [
? ?Components({
? ? ?resolvers: [ElementPlusResolver()],
? }),
],
}
//main.ts
import { createApp } from 'vue'
import App from './App.vue'
?
import { Edit,Search } from '@element-plus/icons' ?//图标需要分开导入,按需导入图标
import { ElButton } from 'element-plus'; ? //按需导入
?
const app = createApp(App);
//注册组件
app.component("edit", Edit)
app.component("search", Search)
app.component('ElButton',ElButton)
app.mount('#app');
<template>
<h2>home页面</h2>
? ?<el-button type="primary" >主要按钮</el-button>
? ?<el-button type="success" >成功按钮</el-button>
? ?<el-icon :size="20" :color="'blue'">
? ? ? ?<edit />
? ?</el-icon>
? ?<el-icon :size="20">
? ? ? ?<search></search>
? ?</el-icon>
</template>
<script setup lang="ts">
</script>
全局导入
推荐添加
// tsconfig.json
{
?"compilerOptions": {
? ?// ...
? ?"types": ["element-plus/global"]
}
}
安装
npm install element-plus --save
# or
yarn add element-plus
?
# 安装icon图标依赖库
npm install @element-plus/icons
# or
yarn add @element-plus/icons
在main.ts 文件中全局配置
import { createApp } from 'vue'
import App from './App.vue'
import { store, key } from './store';
// 注入路由
import router from './router';
?
// 全局引入ui库
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
?
const app = createApp(App);
app.use(store, key);
app.use(router);
app.use(ElementPlus);
app.mount('#app');
使用ui组件
使用图标,因为图标和普通ui组件不是同一个包,使用需要分别导入
//导入具体的组件后直接使用
<template>
? ?<el-icon :size="20" :color="'blue'">
? ? ? ?<edit />
? ?</el-icon>
</template>
<script setup lang="ts">
? ?import { Edit } from '@element-plus/icons'
</script>
将图标库在main.ts文件中impott并使用app.component()注册便可以直接在组件中使用了,和普通的使用ui库同理
<template>
<h2>home页面</h2>
? ?<el-button type="primary" >主要按钮</el-button>
? ?<el-button type="success" >成功按钮</el-button>
? ?<el-icon :size="20" :color="'blue'">
? ? ? ?<edit />
? ?</el-icon>
? ?<el-icon :size="20">
? ? ? ?<search></search>
? ?</el-icon>
</template>
<script setup lang="ts">
</script>
来源:https://juejin.cn/post/7017103423107497992


猜你喜欢
- 前言如果你之前没用过进度条,八成是觉得它会增加不必要的复杂性或者很难维护,其实不然。要加一个进度条其实只需要几行代码。from alive_
- 简介如果你经常网上冲浪,这样参差不齐的多栏布局,是不是很眼熟啊?类似的布局,似乎一夜之间出现在国内外大大小小的网站上,比如 Pinteres
- 说起模板引擎,很多人会认为这是后台的东西(如PHP的Smarty、Java的Velocity),跟前端没有关系。然而,随着前端的逻辑变得越来
- 这篇文章主要介绍了python正则表达式匹配IP代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 前言在日常开发中,客户端上传文件的一般流程是:客户端向服务端发送文件,再由服务端将文件转储到专门的存储服务器或云计算厂商的储存服务(例如阿里
- 前言随着人工智能研究的不断兴起,Python的应用也在不断上升,由于Python语言的简洁性、易读性以及可扩展性,特别是在开源工具和深度学习
- vue3打包过后空白页面在项目根目录下,新建一个vue.config.js文件module.exports = { // 基本路
- 在Web上使用菜单可以极大地节约页面的空间,同时也比较的符合用户从Windows上继承下来的UI操作体验。在以往的Web页菜单设计中,我们普
- 本文实例讲述了python函数装饰器用法。分享给大家供大家参考。具体如下:装饰器经常被用于有切面需求的场景,较为经典的有插入日志、性能测试、
- 本文实例讲述了Python写入CSV文件的方法。分享给大家供大家参考。具体如下:# _*_ coding:utf-8 _*_#xiaohei
- Abs (数值)绝对值。一个数字的绝对值是它的正值。空字符串 (null) 的绝对值,也是空字符串。未初始化的变数,其绝对为 0例子:ABS
- 目录现象根因分析getLastPacketReceivedTimeMs()方法调用时机解决方案现象应用升级MySQL驱动8.0后,在并发量较
- 我们在做表单的时候经常会使用到这样的结构:<fieldset> <lege
- 1. 引言在Python中我们经常使用pip来安装第三方Python软件包,其实我们每个人都可以免费地将自己写的Python包发布到PyPI
- JavaScript 有三种弹窗 Alert (只有确定按钮), Confirmation (确定,取消等按钮), Prompt (有输入对
- 本文实例分析了GO语言异常处理机制panic和recover。分享给大家供大家参考。具体如下:Golang 有2个内置的函数 panic()
- vue中,我们构建单页面应用时候,一定必不可少用到vue-routervue-router 就是我们的路由,这个由vue官方提供的插件首先在
- 假设我们已经安装好了tensorflow。一般在安装好tensorflow后,都会跑它的demo,而最常见的demo就是手写数字识别的dem
- 一、实战场景Flask 框架实现用户的注册,登录和登出。二、主要知识点flask_login 插件使用SQLAlchemy 基础操作用户基础
- 本文实例分析了JS重载实现方法。分享给大家供大家参考,具体如下:重载是面向对象语言里很重要的一个特性,JS中没有真正的重载,是模拟出来的(因