Vue3 introduces the Element-Plus component library vue-cli automatically imports on demand + introduces the Icon icon of ElementPlus

One: Import the Element-Plus component library vue-cli to automatically import on demand

1. Install Element Plus using a package manager (such as NPM, Yarn)

# NPM
$ npm install element-plus --save

# Yarn
$ yarn add element-plus

2. First install two plug-ins unplugin-vue-componentsand unplugin-auto-import:

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

# Yarn
$ yarn add -D unplugin-vue-components unplugin-auto-import

3. vue.config.jsAdd pluginsthe code as follows

const AutoImport = require("unplugin-auto-import/webpack");
const Components = require("unplugin-vue-components/webpack");
const {
    
     ElementPlusResolver } = require("unplugin-vue-components/resolvers");

module.exports = defineConfig({
    
    
  configureWebpack: {
    
    
    plugins: [
      AutoImport({
    
    
        resolvers: [ElementPlusResolver()],
      }),
      Components({
    
    
        resolvers: [ElementPlusResolver()],
      }),
    ],
  },
});

4. Add the following to the vue page to test whether the introduction is successful

<template>
  <div>
    <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>
  </div>
</template>

5. Restart the running project, and then preview the screenshot of the browser as follows

insert image description here

Two: Introduce the Icon icon of elementPlus

1. Run in the project terminal

# NPM
$ npm install unplugin-icons -D

# Yarn
$ yarn add unplugin-icons -D

2. Introduce the full amount of Icon icons in main.ts

import * as ElementPlusIconsVue from "@element-plus/icons-vue";

const app = createApp(App);

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    
    
  app.component(key, component);
}
app.mount("#app");

3. Use it on the vue page

 <el-icon><Expand /></el-icon>

insert image description here

important point:

1. ElementPlus official website address: https://element-plus.org/zh-CN/guide/installation.html#%E4%BD%BF%E7%94%A8%E5%8C%85%E7%AE%A1 %E7%90%86%E5%99%A8

Guess you like

Origin blog.csdn.net/weixin_43861689/article/details/130146890