[web教学] vue3使用element-plus

[复制链接]
查看955 | 回复0 | 2023-8-23 12:13:42 | 显示全部楼层 |阅读模式 来自 中国北京
element-ui 是共同 vue2 使用,element-plus 是设置 vue3 使用的
  1. 安装

1. 包管理器的方式
如果是使用 webpack 大概 vite 打包工具新建的项目
  1. # NPM
  2. npm install element-plus --save
  3. # Yarn
  4. yarn add element-plus
复制代码
2. 欣赏器直接导入
直接通过欣赏器的 HTML 标签导入 Element Plus,然后就可以使用全局变量 ElementPlus
  1. <head>
  2.   <!-- 导入样式 -->
  3.   <link rel="stylesheet" href="//unpkg.com/element-plus/dist/index.css" />
  4.   <!-- 导入 Vue 3 -->
  5.   <script src="//unpkg.com/vue@3"></script>
  6.   <!-- 导入组件库 -->
  7.   <script src="//unpkg.com/element-plus"></script>
  8. </head>
复制代码
2. 导入使用

1. 导入全部组件且注册所有的图标

  1. // 导入 element-plus
  2. import ElementPlus from "element-plus";
  3. // 导入 element-plus 样式
  4. import "element-plus/dist/index.css";
  5. // 导入 element-plus 图标
  6. import * as ElementPlusIconsVue from "@element-plus/icons-vue";
  7. // 注册所有图标
  8. for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  9.   app.component(key, component);
  10. }
复制代码
声明使用 ElementPlus 全局变量
  1. // 使用router\vuex\element-plus并挂载
  2. app.use(ElementPlus).use(router).use(store).mount("#app");
复制代码
项目完备的 main.js 文件
  1. // 导入 vue 和 createApp 方法import App from "./App.vue";import { createApp } from "vue";// 导入路由表import router from "../router/index";// 导入 vueximport store from "../store/index";// 导入 element-plusimport ElementPlus from "element-plus";// 导入 element-plus 样式import "element-plus/dist/index.css";// 导入 element-plus 图标import * as ElementPlusIconsVue from "@element-plus/icons-vue";// 导入全局样式import "@/styles/index.scss";// 创建 vue 实例const app = createApp(App);// 使用router\vuex\element-plus并挂载
  2. app.use(ElementPlus).use(router).use(store).mount("#app");// 注册所有图标for (const [key, component] of Object.entries(ElementPlusIconsVue)) {  app.component(key, component);}
复制代码
2. 按需导入

完成这个功能需要插件的支持,想相识更多插件的知识 ->webpack_插件plugin_游小北的博客-CSDN博客大概webpack打包工具 & 搭建vue项目_游小北的博客-CSDN博客
1. 安装插件
  1. npm install -D unplugin-vue-components unplugin-auto-import
复制代码
2. 在 webpack.config.js 里添加如下设置
  1. // webpack.config.js
  2. const AutoImport = require('unplugin-auto-import/webpack')
  3. const Components = require('unplugin-vue-components/webpack')
  4. const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')
  5. module.exports = {
  6.   // ...
  7.   plugins: [
  8.     AutoImport({
  9.       resolvers: [ElementPlusResolver()],
  10.     }),
  11.     Components({
  12.       resolvers: [ElementPlusResolver()],
  13.     }),
  14.   ],
  15. }
复制代码
如果使用的是 vite 
  1. // vite.config.ts
  2. import { defineConfig } from 'vite'
  3. import AutoImport from 'unplugin-auto-import/vite'
  4. import Components from 'unplugin-vue-components/vite'
  5. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  6. export default defineConfig({
  7.   // ...
  8.   plugins: [
  9.     // ...
  10.     AutoImport({
  11.       resolvers: [ElementPlusResolver()],
  12.     }),
  13.     Components({
  14.       resolvers: [ElementPlusResolver()],
  15.     }),
  16.   ],
  17. })
复制代码
3. elemetn-plus 图标的使用

安装
1. 包管理器方式
  1. # NPM
  2. npm install @element-plus/icons-vue
  3. # Yarn
  4. yarn add @element-plus/icons-vue
  5. # pnpm
  6. pnpm install @element-plus/icons-vue
复制代码
2. 标签导入
通过欣赏器的 HTML 标签导入,使用全局变量 ElementPlusIconsVue。
  1. <script src="//unpkg.com/@element-plus/icons-vue"></script> //unpkg
  2. <script src="//cdn.jsdelivr.net/npm/@element-plus/icons-vue"></script> //jsDelivr
复制代码
3. 使用方式
以标签的形式直接使用
  1. <el-icon><Plus /></el-icon>
  2. <el-icon><Minus /></el-icon>
  3. <el-icon><CirclePlus /></el-icon>
  4. <el-icon><Search /></el-icon>
复制代码



来源:https://blog.csdn.net/m0_66492535/article/details/128048780
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则