current position:Home>How to introduce element plus gracefully in vue3 project on demand

How to introduce element plus gracefully in vue3 project on demand

2022-04-29 09:28:50Chives after five

In our project , We don't use it element-plus All in ui Components , If using full import , This will lead to bloated files after the project is packaged , So we usually import on demand .

First you need to install unplugin-vue-components and unplugin-auto-import These two plug-ins

npm install -D unplugin-vue-components unplugin-auto-import

And then if you use webpack Project created , Then you need to webpack.config.js Add the following code to the file

// 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()],
    }),
  ],
}

If you use vue-cli Project created , Then you need to vue.config.js Add the following code to the file

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({
  transpileDependencies: true,
  configureWebpack: {
    plugins: [
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      })
    ]
  }
})

If you use vite Project created , Then you need to vite.config.js Add the following code to the file

// vite.config.ts
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default {
  plugins: [
    // ...
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
}

Just these two steps , You can use... Anywhere in the project element-plus Inside ui Components , very nice

copyright notice
author[Chives after five],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204290735474817.html

Random recommended