溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue3怎么集成Element-plus實現(xiàn)按需自動引入組件

發(fā)布時間:2022-07-11 09:30:41 來源:億速云 閱讀:759 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細介紹“vue3怎么集成Element-plus實現(xiàn)按需自動引入組件”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當(dāng),希望這篇“vue3怎么集成Element-plus實現(xiàn)按需自動引入組件”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

首先下載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ā)時每次使用都要手動在組件中引入對應(yīng)的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>

效果: 

vue3怎么集成Element-plus實現(xiàn)按需自動引入組件

讀到這里,這篇“vue3怎么集成Element-plus實現(xiàn)按需自動引入組件”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI