PrefaceTake Element Plus as an example to configure on-demand loading of components and styles. environment
Installing Element Plus yarn add element-plus # OR npm install element-plus --save Complete introduction import { createApp } from 'vue' import ElementPlus from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; import App from './App.vue'; const app = createApp(App) app.use(ElementPlus) app.mount('#app') It can be seen that the js and css files of Element Plus are quite large in size and time-consuming. Load on demandInstall the vite-plugin-importer plugin Install yarn add vite-plugin-importer # OR npm install vite-plugin-importer --save There is a plugin column on the Vite official website, where you can use recommended community plugins
Configure vite.config.js import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import usePluginImport from 'vite-plugin-importer' // https://vitejs.dev/config/ export default defineConfig({ plugins: [ vue(), usePluginImport({ libraryName: 'element-plus', customStyleName: (name) => { return `element-plus/lib/theme-chalk/${name}.css`; }, }), ], resolve: { alias: { 'vue': 'vue/dist/vue.esm-bundler.js' }, }, }) main.js import { createApp } from 'vue' import App from './App.vue' import { ElButton, ElSelect } from 'element-plus'; const app = createApp(App) app.component(ElButton.name, ElButton); app.component(ElSelect.name, ElSelect); app.mount('#app') Using Install vite-plugin-style-import Install yarn add vite-plugin-style-import -D # OR npm i vite-plugin-style-import -D The Element Plus official website provides the vite-plugin-style-import on-demand loading method. Configuration vite.config.js import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import styleImport from 'vite-plugin-style-import'; // https://vitejs.dev/config/ export default defineConfig({ plugins: [ vue(), styleImport({ libs: [ { libraryName: 'element-plus', esModule: true, ensureStyleFile: true, resolveStyle: (name) => { return `element-plus/lib/theme-chalk/${name}.css`; }, resolveComponent: (name) => { return `element-plus/lib/${name}`; }, }, ], }), ], resolve: { alias: { 'vue': 'vue/dist/vue.esm-bundler.js' }, }, }) main.js import { createApp } from 'vue' import App from './App.vue' import { ElButton, ElSelect } from 'element-plus'; const app = createApp(App) app.component(ElButton.name, ElButton); app.component(ElSelect.name, ElSelect); app.mount('#app') As you can see, Summarize
This is the end of this article about how Vue3 loads third-party component libraries on demand. For more relevant Vue3 on-demand component library loading content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Introduction to the use and difference between in and exists in MySQL
>>: Solution to forgetting the password of the pagoda panel in Linux 3.X/4.x/5.x
Table of contents Preface 1. Install the service ...
What is a big deal? Transactions that run for a l...
1. Install nginx in docker: It is very simple to ...
Table of contents 1. CentOS7+MySQL8.0, yum source...
Preface Let me share with you how to make a searc...
1. MySQL download address; http://ftp.ntu.edu.tw/...
Preface Because of project needs, the storage fie...
<br />Previous article: Web Design Tutorial ...
The first tutorial for installing MySQL-5.7.19 ve...
Table of contents Preface 1. Props, $emit one-way...
Table of contents Overview Require URL of the app...
1. Meaning of partition table A partition table d...
When we preview PDF on the page, some files canno...
1. On a networked machine, use the default centos...
As the company's influence grows and its prod...