element-plus中如何實現(xiàn)按需導入與全局導入
按需導入:
安裝插件
首先需要引入額外的插件:前**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' ?//圖標需要分開導入,按需導入圖標
import { ElButton } from 'element-plus'; ? //按需導入
?
const app = createApp(App);
//注冊組件
app.component("edit", Edit)
app.component("search", Search)
app.component('ElButton',ElButton)
app.mount('#app');
<template>
<h2>home頁面</h2>
? ?<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>
全局導入
推薦添加
// tsconfig.json
{
?"compilerOptions": {
? ?// ...
? ?"types": ["element-plus/global"]
}
}
安裝
npm install element-plus --save # or yarn add element-plus ? # 安裝icon圖標依賴庫 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庫
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組件
使用圖標,因為圖標和普通ui組件不是同一個包,使用需要分別導入
//導入具體的組件后直接使用
<template>
? ?<el-icon :size="20" :color="'blue'">
? ? ? ?<edit />
? ?</el-icon>
</template>
<script setup lang="ts">
? ?import { Edit } from '@element-plus/icons'
</script>
將圖標庫在main.ts文件中impott并使用app.component()注冊便可以直接在組件中使用了,和普通的使用ui庫同理
<template>
<h2>home頁面</h2>
? ?<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>
到此這篇關于element-plus中如何實現(xiàn)按需導入與全局導入的文章就介紹到這了,更多相關element-plus 按需導入與全局導入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue使用sass根據(jù)環(huán)境進行樣式判斷區(qū)分方式
這篇文章主要介紹了vue使用sass根據(jù)環(huán)境進行樣式判斷區(qū)分方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
vue3組合式api實現(xiàn)v-lazy圖片懶加載的方法實例
vue作為前端主流的3大框架之一,目前在國內(nèi)有著非常廣泛的應用,下面這篇文章主要給大家介紹了關于vue3組合式api實現(xiàn)v-lazy圖片懶加載的相關資料,需要的朋友可以參考下2022-09-09

