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

Example of using Dockerfile to build an nginx image

Introduction to Dockerfile Docker can automatical...

CentOS7 upgrade kernel kernel5.0 version

Upgrade process: Original system: CentOS7.3 [root...

Use simple jQuery + CSS to create a custom a tag title tooltip

Introduction Use simple jQuery+CSS to create a cus...

How to use @media in mobile adaptive styles

General mobile phone style: @media all and (orien...

How to use node to implement static file caching

Table of contents cache Cache location classifica...

Graphical steps of zabbix monitoring vmware exsi host

1. Enter the virtualization vcenter, log in with ...

CSS selects the first child element under the parent element (:first-child)

Preface I recently used :first-child in a project...

Linux platform mysql enable remote login

During the development process, I often encounter...

zabbix custom monitoring nginx status implementation process

Table of contents Zabbix custom monitoring nginx ...

Summary of 6 skills needed to master web page production

It has to be said that a web designer is a general...

Nginx service 500: Internal Server Error one of the reasons

500 (Internal Server Error) The server encountere...

Detailed explanation of keepAlive usage in Vue front-end development

Table of contents Preface keep-avlive hook functi...