Vue3項(xiàng)目中引入ElementUI并使用的示例詳解
Vue3項(xiàng)目中引入 ElementUI
ElementUI是一個強(qiáng)大的PC端UI組件框架,它不依賴于vue,但是卻是當(dāng)前和vue配合做項(xiàng)目開發(fā)的一個比較好的ui框架,其包含了布局(layout),容器(container)等各類組件,基本上能滿足日常網(wǎng)站的搭建開發(fā)。針對vue2和vue3都有對應(yīng)的組件版本,本文主要介紹在vue3中如何引入使用ElementUI(vue3中升級為Element Plus)。
1.安裝
vue3中使用如下命令通過 npm 安裝 ECharts(本人項(xiàng)目使用的安裝方式)
$ npm install element-plus --save
也可以使用其他的包管理起進(jìn)行安裝:
# Yarn $ yarn add element-plus # pnpm $ pnpm install element-plus
2.引入
element-plus分為全局引入和按需引入兩種方式,一般在工程項(xiàng)目中,由于全局引入會導(dǎo)致不必要的資源加載,為提升項(xiàng)目性能,建議進(jìn)行按需引入。以下我們對兩種引入方式進(jìn)行介紹。
2.1 全局引入
全局引入就是在項(xiàng)目入口(main.ts)文件直接引入組件以及組件全部的樣式文件;代碼如下所示:
// main.ts 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')
2.2 按需引入
在vue3中按需引入ElementUI,需要使用其他的插件輔助,需要安裝unplugin-vue-components 和 unplugin-auto-import這兩款插件;安裝方法如下:
npm install -D unplugin-vue-components unplugin-auto-import
然后再vite或者webpack配置中添加相應(yīng)的配置,如下所示:
vite
// vite.config.ts import { defineConfig } from 'vite' 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: [ // ... AutoImport({ resolvers: [ElementPlusResolver()], }), Components({ resolvers: [ElementPlusResolver()], }), ], })
Webpack
// webpack.config.js const AutoImport = require('unplugin-auto-import/webpack') const Components = require('unplugin-vue-components/webpack') const { ElementPlusResolver } = require('unplugin-vue-components/resolvers') module.exports = { // ... plugins: [ AutoImport({ resolvers: [ElementPlusResolver()], }), Components({ resolvers: [ElementPlusResolver()], }), ], }
若篇日志再vue.config.js中,導(dǎo)入方法相同:
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') module.exports = defineConfig({ configureWebpack: { plugins: [ AutoImport({ resolvers: [ElementPlusResolver()], }), Components({ resolvers: [ElementPlusResolver()], }), ], } })
3.使用
引入完畢之后,我們可進(jìn)行按需引入需要使用的組件,使用方法如下,引入input組件和button組件
<template> <div> <el-input class="input" v-model="input" type="file" placeholder="Please input" /> <el-button class="button" type="primary">文件處理</el-button> </div> </template> <script> import { ElButton, ElInput } from 'element-plus' import { ref } from 'vue' export default { components: { ElButton,ElInput }, } </script> <style scoped> .input { display: inline; margin: 20px 30px; } .button { width: 90px; } </style>
效果如下:
此外ElementUI中還有其他組件,基本能滿足開發(fā)需求,提升開發(fā)效率,詳情請見官網(wǎng):ElementUI
注:在vue3中,由于vite打包擁有良好的性能,本文使用的示例為vite打包方式,同時建議使用其他包最新的支持版本進(jìn)行開發(fā)。
以上就是Vue3項(xiàng)目中引入ElementUI并使用的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Vue3 ElementUI的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue+webpack實(shí)現(xiàn)懶加載過程解析
這篇文章主要介紹了Vue+webpack實(shí)現(xiàn)懶加載過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02vue+elementUI動態(tài)生成面包屑導(dǎo)航教程
今天小編就為大家分享一篇vue+elementUI動態(tài)生成面包屑導(dǎo)航教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11vue實(shí)現(xiàn)點(diǎn)擊按鈕切換背景顏色的示例代碼
這篇文章主要介紹了用vue簡單的實(shí)現(xiàn)點(diǎn)擊按鈕切換背景顏色,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06vue項(xiàng)目中axios請求網(wǎng)絡(luò)接口封裝的示例代碼
這篇文章主要介紹了vue項(xiàng)目中axios請求網(wǎng)絡(luò)接口封裝的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12vuex項(xiàng)目中登錄狀態(tài)管理的實(shí)踐過程
由于狀態(tài)零散地分布在許多組件和組件之間的交互中,大型應(yīng)用復(fù)雜度也經(jīng)常逐漸增長,為了解決這個問題,Vue 提供 vuex,這篇文章主要給大家介紹了關(guān)于vuex項(xiàng)目中登錄狀態(tài)管理的相關(guān)資料,需要的朋友可以參考下2021-09-09vue中同時監(jiān)聽多個參數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了vue中同時監(jiān)聽多個參數(shù)的實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04vue使用smooth-signature實(shí)現(xiàn)移動端橫豎屏電子簽字
這篇文章主要為大家介紹了vue使用smooth-signature實(shí)現(xiàn)移動端橫豎屏電子簽字示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10