欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3集成Element-plus實(shí)現(xiàn)按需自動(dòng)引入組件的方法總結(jié)

 更新時(shí)間:2022年07月10日 15:31:31   作者:小王同學(xué)|  
vue3出來一段時(shí)間了,element也更新了版本去兼容vue3,下面這篇文章主要給大家介紹了關(guān)于vue3集成Element-plus實(shí)現(xiàn)按需自動(dòng)引入組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
  • element-plus正是element-ui針對(duì)于vue3開發(fā)的一個(gè)UI組件庫,
  • 它的使用方式和很多其他的組件庫是一樣的,其他類似于ant-design-vue、NaiveUI、VantUI都是差不多的;安裝element-plus

首先下載element-plus

npm install element-plus

1、第一種方式,使用全局引入

引入element-plus的方式是全局引入,代表的含義是所有的組件和插件都會(huì)被自動(dòng)注冊(cè),

優(yōu)點(diǎn):上手快

缺點(diǎn):會(huì)增大包的體積

在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ā)中用到某個(gè)組件對(duì)某個(gè)組件進(jìn)行引入,

<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>

但是這樣我們?cè)陂_發(fā)時(shí)每次使用都要手動(dòng)在組件中引入對(duì)應(yīng)的css樣式,使用起來會(huì)比較麻煩

3、按需自動(dòng)引入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屬性完全一致,最后會(huì)進(jìn)行合并
  configureWebpack: {
    resolve: {
      alias: {
        components: '@/components'
      }
    },
    //配置webpack自動(dòng)按需引入element-plus,
      plugins: [
        AutoImport({
          resolvers: [ElementPlusResolver()]
        }),
        Components({
          resolvers: [ElementPlusResolver()]
        })
      ]
  }
}

 按需自動(dòng)引入配置完之后,在組件中可直接使用,不需要引用和注冊(cè) 這里已經(jīng)實(shí)現(xiàn)了按需自動(dòng)移入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é)

到此這篇關(guān)于vue3集成Element-plus實(shí)現(xiàn)按需自動(dòng)引入組件的文章就介紹到這了,更多相關(guān)vue3按需自動(dòng)引入組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論