How to implement on-demand import and global import in element-plus

How to implement on-demand import and global import in element-plus

Import on demand:

Install the plugin

First, you need to introduce additional plug-ins: the former ** vite-plugin-components has been renamed to unplugin-vue-components **

npm install unplugin-vue-components

Configuring the plugin

Add configuration in the weapack or vite configuration file

// vite.config.ts
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
​
export default {
  plugins: [
   // ...
   Components({
    resolvers: [ElementPlusResolver()],
  }),
  ],
}
// webpack.config.js
const Components = require('unplugin-vue-components/webpack')
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')
​
module.exports = {
  // ...
  plugins: [
   Components({
    resolvers: [ElementPlusResolver()],
  }),
  ],
}
//main.ts
import { createApp } from 'vue'
import App from './App.vue'
​
import { Edit,Search } from '@element-plus/icons' //Icons need to be imported separately, import icons on demand import { ElButton } from 'element-plus'; //Import on demand
const app = createApp(App);
//Register component app.component("edit", Edit)
app.component("search", Search)
app.component('ElButton',ElButton)
app.mount('#app');
<template>
    <h2>Home Page</h2>
   <el-button type="primary" >Primary button</el-button>
   <el-button type="success" >Success button</el-button>
   <el-icon :size="20" :color="'blue'">
     <edit />
   </el-icon>
   <el-icon :size="20">
     <search></search>
   </el-icon>
</template>
<script setup lang="ts"> 
</script>

Global Import

Recommended Additions

// tsconfig.json
{
  "compilerOptions": {
   // ...
   "types": ["element-plus/global"]
  }
}

Install

npm install element-plus --save
# or
yarn add element-plus
​
# Install icon icon dependency library npm install @element-plus/icons
# or
yarn add @element-plus/icons

Global configuration in main.ts file

import { createApp } from 'vue'
import App from './App.vue'
import { store, key } from './store';
//Inject routingimport router from './router';
​
// Globally import the UI library import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
​
const app = createApp(App);
app.use(store, key);
app.use(router);
app.use(ElementPlus);
app.mount('#app');

Using UI components

Use icons, because icons and ordinary UI components are not in the same package, they need to be imported separately

//Use <template> directly after importing specific components
   <el-icon :size="20" :color="'blue'">
     <edit />
   </el-icon>
</template>
<script setup lang="ts">
   import { Edit } from '@element-plus/icons'
</script>

Impott the icon library in the main.ts file and register it with app.component(), then you can use it directly in the component, just like using a normal UI library.

<template>
    <h2>Home Page</h2>
   <el-button type="primary" >Primary button</el-button>
   <el-button type="success" >Success button</el-button>
   <el-icon :size="20" :color="'blue'">
     <edit />
   </el-icon>
   <el-icon :size="20">
     <search></search>
   </el-icon>
</template>
<script setup lang="ts"> 
</script>

This is the end of this article about how to implement on-demand import and global import in element-plus. For more relevant content about element-plus on-demand import and global import, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation of importing and exporting vue-element-admin projects
  • How to import Element UI components using vue-cli
  • A brief discussion on how to import css library (elementUi) into vue.js
  • vue + element-ui realizes simple import and export functions

<<:  Recommended 20 best free English handwriting fonts

>>:  How to use MyCat to implement MySQL master-slave read-write separation in Linux

Recommend

How to simply encapsulate axios in vue

Inject axios into Vue import axios from 'axio...

mysql server is running with the --skip-grant-tables option

The MySQL server is running with the --skip-grant...

How to start a Java program in docker

Create a simple Spring boot web project Use the i...

Problem record of using vue+echarts chart

Preface echarts is my most commonly used charting...

React Synthetic Events Explained

Table of contents Start by clicking the input box...

JavaScript to achieve elastic navigation effect

This article shares the specific code for JavaScr...

How to transfer files between Windows and Linux

File transfer between Windows and Linux (1) Use W...

Two ways to install Python3 on Linux servers

First method Alibaba Cloud and Baidu Cloud server...

Detailed explanation of JQuery selector

Table of contents Basic selectors: Level selector...

The difference between useEffect and useLayoutEffect in React

Table of contents Prerequisites useEffect commitB...

Detailed explanation of the general steps for SQL statement optimization

Preface This article mainly shares with you the g...

Summary of common commands for Ubuntu servers

Most of the commands below need to be entered in ...

A brief discussion on the magical slash in nginx reverse proxy

When configuring nginx reverse proxy, the slashes...