vue3集成Element-Plus之全局導(dǎo)入和按需導(dǎo)入
element-plus集成
Element Plus,一套為開發(fā)者、設(shè)計(jì)師和產(chǎn)品經(jīng)理準(zhǔn)備的基于 Vue 3.0 的桌面端組件庫(kù):
- 在Vue2中使用element-ui,而element-plus是element-ui針對(duì)于vue3開發(fā)的一個(gè)UI組件庫(kù);
- 它的使用方式和很多其他的組件庫(kù)是一樣的,所以學(xué)會(huì)element-plus,其他類似于ant-design-vue、NaiveUI、VantUI都是差不多的;
- 移動(dòng)端使用VantUI | MintUI
- 安裝element-plus
npm install element-plus
1. 全局引入
一種引入element-plus的方式是全局引入,代表的含義是所有的組件和插件都會(huì)被自動(dòng)注冊(cè):
//main.ts import { createApp } from 'vue'; import App from './App.vue'; import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' import router from './router' import store from './store' createApp(App).use(router).use(store).use(ElementPlus).mount('#app')
2. 局部引入(按需引入)
也就是在開發(fā)中用到某個(gè)組件對(duì)某個(gè)組件進(jìn)行引入:
2.1 手動(dòng)引入
<template> <div id="app"> <el-row class="mb-4"> <el-button disabled>Default</el-button> <el-button type="primary" disabled>Primary</el-button> <el-button type="success" disabled>Success</el-button> <el-button type="info" disabled>Info</el-button> <el-button type="warning" disabled>Warning</el-button> <el-button type="danger" disabled>Danger</el-button> </el-row> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' import { ElButton } from 'element-plus' export default defineComponent({ name: 'App', components: { ElButton } }) </script> <style lang="less"> </style>
但是我們會(huì)發(fā)現(xiàn)是沒(méi)有對(duì)應(yīng)的樣式的,引入樣式有兩種方式:
全局引用樣式;import 'element-plus/dist/index.css'
局部引用樣式(通過(guò) unplugin-element-plus 插件);
1.安裝插件:
npm install unplugin-element-plus -D
2.配置vue.config.js
const ElementPlus= require('unplugin-element-plus/webpack'); module.exports = { configureWebpack: { resolve: { alias: { components: '@/components' } }, //配置webpack自動(dòng)按需引入element-plus樣式, plugins: [ElementPlus()] } };
但是這里依然有個(gè)弊端:
- 這些組件我們?cè)诙鄠€(gè)頁(yè)面或者組件中使用的時(shí)候,都需要導(dǎo)入并且在components中進(jìn)行注冊(cè);
- 所以我們可以將它們?cè)?strong>全局注冊(cè)一次;
import { ElButton, ElTable, ElAlert, ElAside, ElAutocomplete, ElAvatar, ElBacktop, ElBadge, } from 'element-plus' const app = createApp(App) const components = [ ElButton, ElTable, ElAlert, ElAside, ElAutocomplete, ElAvatar, ElBacktop, ElBadge ] for (const cpn of components) { app.component(cpn.name, cpn) }
2.3 自動(dòng)導(dǎo)入組件以及樣式[推薦】
1.安裝插件:
npm install -D unplugin-vue-components unplugin-auto-import
2.配置vue.config.js(其他配置方式看官網(wǎng))
const AutoImport = require('unplugin-auto-import/webpack'); const Components = require('unplugin-vue-components/webpack'); const { ElementPlusResolver } = require('unplugin-vue-components/resolvers'); module.exports = { configureWebpack: { resolve: { alias: { components: '@/components' } }, //配置webpack自動(dòng)按需引入element-plus, plugins: [ AutoImport({ resolvers: [ElementPlusResolver()] }), Components({ resolvers: [ElementPlusResolver()] }) ] } };
3 直接使用
<template> <div id="app"> <el-row class="mb-4"> <el-button disabled>Default</el-button> <el-button type="primary" disabled>Primary</el-button> <el-button type="success" disabled>Success</el-button> <el-button type="info" disabled>Info</el-button> <el-button type="warning" disabled>Warning</el-button> <el-button type="danger" disabled>Danger</el-button> </el-row> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ }) </script> <style lang="less"> </style>
總結(jié)
到此這篇關(guān)于vue3集成Element-Plus之全局導(dǎo)入和按需導(dǎo)入的文章就介紹到這了,更多相關(guān)Element-Plus全局導(dǎo)入和按需導(dǎo)入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何通過(guò)Vue自定義指令實(shí)現(xiàn)前端埋點(diǎn)詳析
埋點(diǎn)分析是網(wǎng)站分析的一種常用的數(shù)據(jù)采集方法,下面這篇文章主要給大家介紹了關(guān)于如何通過(guò)Vue自定義指令實(shí)現(xiàn)前端埋點(diǎn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07基于Vue實(shí)現(xiàn)封裝一個(gè)虛擬列表組件
正常情況下,我們對(duì)于數(shù)據(jù)都會(huì)分頁(yè)加載,最近項(xiàng)目中確實(shí)遇到了不能分頁(yè)的場(chǎng)景,如果不分頁(yè),頁(yè)面渲染幾千條數(shù)據(jù)就會(huì)感知到卡頓,使用虛擬列表就勢(shì)在必行了。本文主要介紹了如何基于Vue實(shí)現(xiàn)封裝一個(gè)虛擬列表組件,感興趣的可以了解一下2023-03-03vue3中g(shù)etCurrentInstance不推薦使用及在<script?setup>中獲取全局內(nèi)容的三種方式
這篇文章主要給大家介紹了關(guān)于vue3中g(shù)etCurrentInstance不推薦使用及在<script?setup>中獲取全局內(nèi)容的三種方式,文中通過(guò)介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-02-02Javascript結(jié)合Vue實(shí)現(xiàn)對(duì)任意迷宮圖片的自動(dòng)尋路
本文將結(jié)合實(shí)例代碼介紹Javascript結(jié)合Vue實(shí)現(xiàn)對(duì)任意迷宮圖片的自動(dòng)尋路,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06vue2 中如何實(shí)現(xiàn)動(dòng)態(tài)表單增刪改查實(shí)例
本篇文章主要介紹了vue2 中如何實(shí)現(xiàn)動(dòng)態(tài)表單增刪改查實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06vue中使用echarts并根據(jù)選擇條件動(dòng)態(tài)展示echarts圖表
雖然老早就看過(guò)很多echarts的例子, 但自己接觸的項(xiàng)目中一直都沒(méi)有真正用到過(guò),直到最近才開始真正使用,下面這篇文章主要給大家介紹了關(guān)于vue中使用echarts并根據(jù)選擇條件動(dòng)態(tài)展示echarts圖表的相關(guān)資料,需要的朋友可以參考下2023-12-12Vue?中的?computed?和?watch?的區(qū)別詳解
這篇文章主要介紹了Vue中的computed和watch的區(qū)別詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09