vue3集成Element-plus實現(xiàn)按需自動引入組件的方法總結(jié)
- element-plus正是element-ui針對于vue3開發(fā)的一個UI組件庫,
- 它的使用方式和很多其他的組件庫是一樣的,其他類似于ant-design-vue、NaiveUI、VantUI都是差不多的;安裝element-plus
首先下載element-plus
npm install element-plus
1、第一種方式,使用全局引入
引入element-plus的方式是全局引入,代表的含義是所有的組件和插件都會被自動注冊,
優(yōu)點:上手快
缺點:會增大包的體積
在main.ts文件中
import { createApp } from 'vue' // 全局引入 import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' import App from './App.vue' import router from './router' import store from './store' const app = createApp(App) app.use(router) app.use(store) app.use(ElementPlus) app.mount('#app')
2、第二種方式,使用局部引入
局部引入也就是在開發(fā)中用到某個組件對某個組件進行引入,
<template> <div class="app"> <el-button>Default</el-button> <el-button type="primary">Primary</el-button> <el-button type="success">Success</el-button> <el-button type="info">Info</el-button> <el-button type="warning">Warning</el-button> <el-button type="danger">Danger</el-button> <el-button>中文</el-button> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' // 局部引入 import { ElButton } from 'element-plus' import 'element-plus/theme-chalk/el-button.css' import 'element-plus/theme-chalk/base.css' export default defineComponent({ components: { ElButton }, setup() { return {} } }) </script> <style lang="less"></style>
但是這樣我們在開發(fā)時每次使用都要手動在組件中引入對應的css樣式,使用起來會比較麻煩
3、按需自動引入element-plus 推薦
需要安裝unplugin-vue-components
和 unplugin-auto-import
這兩款插件
npm install -D unplugin-vue-components unplugin-auto-import
安裝完成之后在vue.config.js文件中配置
// vue.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 = { outputDir: './build', // 和webpapck屬性完全一致,最后會進行合并 configureWebpack: { resolve: { alias: { components: '@/components' } }, //配置webpack自動按需引入element-plus, plugins: [ AutoImport({ resolvers: [ElementPlusResolver()] }), Components({ resolvers: [ElementPlusResolver()] }) ] } }
按需自動引入配置完之后,在組件中可直接使用,不需要引用和注冊 這里已經(jīng)實現(xiàn)了按需自動移入Element-plus組件 組件中直接使用:
<template> <div class="app"> <el-button>Default</el-button> <el-button type="primary">Primary</el-button> <el-button type="success">Success</el-button> <el-button type="info">Info</el-button> <el-button type="warning">Warning</el-button> <el-button type="danger">Danger</el-button> <el-button>中文</el-button> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ setup() { return {} } }) </script> <style lang="less"></style>
效果:
總結(jié)
到此這篇關于vue3集成Element-plus實現(xiàn)按需自動引入組件的文章就介紹到這了,更多相關vue3按需自動引入組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
ant-design-vue 實現(xiàn)表格內(nèi)部字段驗證功能
這篇文章主要介紹了ant-design-vue 實現(xiàn)表格內(nèi)部字段驗證功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12Vue中使用localStorage存儲token并設置時效
這篇文章主要為大家介紹了Vue中使用localStorage存儲token并設置時效,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06使用vue-cli創(chuàng)建vue2項目的實戰(zhàn)步驟詳解
相信大部分Vue開發(fā)者都使用過vue-cli來構(gòu)建項目,它的確很方便,但對于很多初級開發(fā)者來說,還是要踩不少坑的,下面這篇文章主要給大家介紹了關于使用vue-cli創(chuàng)建vue2項目的實戰(zhàn)步驟,需要的朋友可以參考下2023-01-01