溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

element-plus中怎么實(shí)現(xiàn)按需導(dǎo)入與全局導(dǎo)入

發(fā)布時(shí)間:2021-11-25 11:11:44 來(lái)源:億速云 閱讀:685 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了element-plus中怎么實(shí)現(xiàn)按需導(dǎo)入與全局導(dǎo)入,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

按需導(dǎo)入:

安裝插件

首先需要引入額外的插件:前**vite-plugin-components已重命名為unplugin-vue-components**

npm install unplugin-vue-components

配置插件

在weapack或vite配置文件內(nèi)中添加配置

// 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'  //圖標(biāo)需要分開(kāi)導(dǎo)入,按需導(dǎo)入圖標(biāo)
import { ElButton } from 'element-plus';   //按需導(dǎo)入

const app = createApp(App);
//注冊(cè)組件
app.component("edit", Edit)
app.component("search", Search)
app.component('ElButton',ElButton)
app.mount('#app');
<template>
    <h3>home頁(yè)面</h3>
    <el-button type="primary" >主要按鈕</el-button>
    <el-button type="success" >成功按鈕</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>

全局導(dǎo)入

推薦添加

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

安裝

npm install element-plus --save
# or
yarn add element-plus

# 安裝icon圖標(biāo)依賴庫(kù)
npm install @element-plus/icons
# or
yarn add @element-plus/icons

在main.ts 文件中全局配置

import { createApp } from 'vue'
import App from './App.vue'
import { store, key } from './store';
// 注入路由
import router from './router';

// 全局引入ui庫(kù)
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');

使用ui組件

使用圖標(biāo),因?yàn)閳D標(biāo)和普通ui組件不是同一個(gè)包,使用需要分別導(dǎo)入

//導(dǎo)入具體的組件后直接使用
<template>
    <el-icon :size="20" :color="'blue'">
        <edit />
    </el-icon>
</template>
<script setup lang="ts">
    import { Edit } from '@element-plus/icons'
</script>

將圖標(biāo)庫(kù)在main.ts文件中impott并使用app.component()注冊(cè)便可以直接在組件中使用了,和普通的使用ui庫(kù)同理

<template>
    <h3>home頁(yè)面</h3>
    <el-button type="primary" >主要按鈕</el-button>
    <el-button type="success" >成功按鈕</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>

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“element-plus中怎么實(shí)現(xiàn)按需導(dǎo)入與全局導(dǎo)入”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI