当前位置: 首页 > news >正文

基于Vue2与3版本的Element UI与Element Plus入门

基于Vue2与3版本的Element UI与Element Plus入门

      • Element UI 入门
        • 安装
        • 引入 Element UI
        • 使用组件
      • Element Plus 入门
        • 安装
        • 引入 Element Plus
        • 使用组件
      • 常用组件
      • 自定义主题
      • 兼容性和升级
      • 社区和支持

Element UI 入门

Element UI 是基于 Vue 2.0 的桌面端组件库,它提供了一系列配套的组件和布局,帮助开发者快速构建功能丰富、美观大方的网页界面。

安装

通过 npm 安装 Element UI:

npm i element-ui -S
引入 Element UI

在 Vue 项目中,可以通过以下几种方式引入 Element UI:

  1. 完整引入:
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
  1. 按需引入(推荐):
    首先,安装 babel-plugin-component
npm install babel-plugin-component -D

然后,在 .babelrcbabel.config.js 文件中添加配置:

{"plugins": [["component",{"libraryName": "element-ui","styleLibraryName": "theme-chalk"}]]
}

最后,在 main.js 中按需引入组件:

import Vue from 'vue';
import { Button, Select } from 'element-ui';
import App from './App.vue';
Vue.component(Button.name, Button);
Vue.component(Select.name, Select);
new Vue({el: '#app',render: h => h(App)
});
使用组件

在 Vue 模板中,可以直接使用 Element UI 的组件:

<template><el-button @click="handleClick" type="primary">主要按钮</el-button>
</template>
<script>
export default {methods: {handleClick() {console.log('按钮被点击');}}
};
</script>

Element Plus 入门

Element Plus 是 Element UI 的 Vue 3 版本,提供了与 Element UI 类似的功能和组件,但针对 Vue 3 进行了优化和适配。

安装

通过 npm 安装 Element Plus:

npm install element-plus --save
引入 Element Plus

在 Vue 3 项目中,可以通过以下几种方式引入 Element Plus:

  1. 完整引入:
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')
  1. 按需引入:
    首先,你需要安装 unplugin-vue-componentsunplugin-auto-import 这两个插件来支持按需引入:
npm install -D unplugin-vue-components unplugin-auto-import

然后,在 Vue CLI 或 Vite 的配置文件中添加插件配置:

// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
export default defineConfig({plugins: [vue(),AutoImport({resolvers: [ElementPlusResolver()],}),Components({resolvers: [ElementPlusResolver()],}),],
})
使用组件

在 Vue 模板中,可以直接使用 Element Plus 的组件:

<template><el-button @click="handleClick" type="primary">主要按钮</el-button>
</template>
<script setup>
import { ref } from 'vue'
const handleClick = () => {console.log('按钮被点击');
}
</script>

常用组件

Element UI 和 Element Plus 包含许多常用的组件,如:

  • Button(按钮)
  • Input(输入框)
  • Select(选择器)
  • Table(表格)
  • Pagination(分页)
  • Dialog(对话框)
  • Form(表单)
    每个组件都有详细的文档和示例,方便开发者查阅和使用。

自定义主题

Element UI 和 Element Plus 都支持自定义主题。你可以通过修改 SCSS 变量来定制主题,也可以使用在线主题生成工具来生成。

兼容性和升级

由于 Element UI 是基于 Vue 2.0 的,因此在使用 Vue 3.0 的项目中,你可能需要考虑使用 Element Plus。同样地,如果你正在使用 Vue 2.0,你应该使用 Element UI。

社区和支持

Element UI 和 Element Plus 都有庞大的社区和良好的文档支持,开发者可以方便地找到问题解决方案和最佳实践。
通过以上步骤,你可以快速地开始使用 Element UI 或 Element Plus 来构建你的 Vue 应用。

http://www.lryc.cn/news/356980.html

相关文章:

  • Mysql数据库创建自增序列
  • macOS上用Qt creator编译并跑shotcut
  • 基于高光谱数据集的创新点实现-高斯核函数卷积神经网络
  • 【python 进阶】 绘图
  • memblock_free_all释放page到buddy,前后nr_free的情况
  • Django实现websocket
  • 先进制造aps专题九 中国aps行业分析
  • 力扣hot100:23. 合并 K 个升序链表
  • Lightweight Robust Size Aware Cache Management——论文泛读
  • 搜索自动补全-elasticsearch实现
  • 连接远程的kafka【linux】
  • 简单的 Cython 示例
  • Laravel时间处理类Carbon
  • 2024年5月软考架构题目回忆分享
  • 香橙派 AIpro开发板初上手
  • 如何使用DotNet-MetaData识别.NET恶意软件源码文件元数据
  • LeetCode---栈与队列
  • 【教程】利用API接口添加本站同款【每日新闻早早报】-每天自动更新,不占用文章数量
  • 僵尸进程,孤儿进程,守护进程
  • Nuxt3 中使用 ESLint
  • 【Jmeter】性能测试之压测脚本生成,也可以录制接口自动化测试场景
  • Go 编程技巧:零拷贝字符串与切片转换的高效秘籍
  • 音视频开发—FFmpeg 音频重采样详解
  • 统计本地端口占用情况
  • 【MySQL精通之路】SQL优化(1)-查询优化(9)-外部联接优化
  • Python应用开发——30天学习Streamlit Python包进行APP的构建(1)
  • 轻兔推荐 —— 一个好用的软件服务推荐平台
  • LeetCode hot100-57-G
  • 基于Vue uni-app的自定义列表表格信息展示组件
  • 计网(部分在session学习章)