vue如何批量引入組件、注冊和使用詳解
前言
組件是我們非常常用的東西,很多人使用組件都是通過一個一個文件去引用和注冊。這篇文章就來介紹下vue批量引入組件、注冊和使用的方法。
一、使用場景
在日常開發(fā)中,經(jīng)常會有這樣一種情況:
import A from 'components/A' import B from 'components/B' import C from 'components/C' import D from 'components/D'
遇到這種重復(fù)的代碼,就在想,是否可以進(jìn)行以下優(yōu)化,一次性全部引入。于是就找到了webpack的api,通過調(diào)用require.context來進(jìn)行處理,具體代碼如下:
二、使用步驟
涉及到:
- 組件名稱為帶中橫線規(guī)范,最后要轉(zhuǎn)為駝峰命名法的功能;
- component的is屬性;
具體詳解都在代碼中:
1.文件目錄

2.HTML代碼
<template>
<div class="water-analysis">
<div class="content-box" ref="contentbox">
<a-tabs :default-active-key="activeComponent" @change="tabChangeHandle">
<a-tab-pane
v-for="item in tabList"
:key="item.key"
:tab="item.tab"
></a-tab-pane>
</a-tabs>
<div class="tab-pane-box">
<!-- 通過is屬性,綁定對應(yīng)的組件名稱,展示對應(yīng)的組件 -->
<component :is="activeComponent"></component>
</div>
</div>
</div>
</template>
3.js代碼
語法:require.context(directory, useSubdirectories, regExp)
- directory: 要查找的文件路徑
- useSubdirectories: 是否查找子目錄
- regExp: 要匹配文件的正則
返回值:兩個方法一個屬性
- keys(): 返回匹配成功模塊的名字組成的數(shù)組
- resolve(): 接受一個參數(shù)request,request為test文件夾下面匹配文件的相對路徑,返回這個匹配文件相對于整個工程的相對路徑
- id:執(zhí)行環(huán)境的id,返回的是一個字符串,主要用在module.hot.accept,應(yīng)該是熱加載
<script>
// 中橫線轉(zhuǎn)駝峰
var camelCase = function (s) {
return s.replace(/-\w/g, function (x) {
return x.slice(1).toUpperCase();
});
};
// 批量引入子組件 重點,語法見上
const allComponents = require.context("./comp", false, /\.vue$/);
console.log(allComponents.keys())
// ["./tem-a.vue", "./tem-b.vue", "./tem-c.vue", "./tem-d.vue"]
console.log(allComponents.id)
//./src/views/tempManage/comp sync \.vue$
//制作組件數(shù)組,在下方components中注冊使用
let resComponents = {};
allComponents.keys().forEach(comName => {
let name = camelCase(comName);
const comp = allComponents(comName);
resComponents[name.replace(/^\.\/(.*)\.\w+$/, "$1")] = comp.default;
});
export default {
name: "WaterQuery",
components: resComponents,
data() {
return {
activeComponent: "temA",
tabList: [
{
key: "temA",
tab: "A組件",
},
{
key: "temB",
tab: "B組件",
},
{
key: "temC",
tab: "C組件",
},
{
key: "temD",
tab: "D組件",
},
],
};
},
created() {
if (this.$route.query["val"]) {
this.activeComponent = this.$route.query["val"];
}
},
methods: {
// 切換tab欄
tabChangeHandle(val) {
const {path} = this.$router;
this.$router.push({
path,
query: {val},
});
this.activeComponent = val;
},
},
};
</script>
4.css代碼(可不看,寫出來只是為了代碼完整性,拿來可以直接運行展示)
<style scoped>
.water-analysis {
height: 100%;
overflow: auto;
}
.content-box {
height: 100%;
}
.tab-pane-box {
height: calc(100% - 62px);
}
</style>
三、總結(jié)
到此這篇關(guān)于vue如何批量引入組件、注冊和使用的文章就介紹到這了,更多相關(guān)vue批量引入組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中el-autocomplete與el-select的異同
本文主要介紹了vue中el-autocomplete與el-select的異同,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
vue.js如何處理數(shù)組對象中某個字段是否變?yōu)閮蓚€字段
這篇文章主要介紹了vue.js如何處理數(shù)組對象中某個字段是否變?yōu)閮蓚€字段方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
vue?調(diào)用瀏覽器攝像頭實現(xiàn)及原理解析
這篇文章主要為大家介紹了vue調(diào)用瀏覽器攝像頭實現(xiàn)及原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
一篇文章帶你吃透Vue生命周期(結(jié)合案例通俗易懂)
這篇文章主要給大家介紹了關(guān)于如何通過一篇文章帶你吃透Vue生命周期,文章通過結(jié)合案例更加的通俗易懂,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-02-02

