PrefaceComponents are something we use very often. Many people use components by referencing and registering them one file at a time. This article will introduce how to batch introduce, register and use components in Vue. 1. Usage scenariosIn daily development, there is often such a situation: import A from 'components/A' import B from 'components/B' import C from 'components/C' import D from 'components/D' When I encountered this kind of repetitive code, I wondered if I could make the following optimizations and import them all at once. So I found the webpack API and processed it by calling require.context. The specific code is as follows: 2. Usage stepsInvolving:
The detailed explanation is in the code: 1. File Directory 2. HTML code <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"> <!-- Through the is attribute, bind the corresponding component name and display the corresponding component--> <component :is="activeComponent"></component> </div> </div> </div> </template> 3.js code Syntax:
Return value: two methods and one property
<script> // Convert the middle horizontal line to camelCase var camelCase = function (s) { return s.replace(/-\w/g, function (x) { return x.slice(1).toUpperCase(); }); }; // Import subcomponents in batches, see the syntax above 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$ // Make a component array and register it in the components below 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: "Component A", }, { key: "temB", tab: "B Component", }, { key: "temC", tab: "C Components", }, { key: "temD", tab: "D Components", }, ], }; }, created() { if (this.$route.query["val"]) { this.activeComponent = this.$route.query["val"]; } }, methods: { // Switch tab bar tabChangeHandle(val) { const {path} = this.$router; this.$router.push({ path, query: {val}, }); this.activeComponent = val; }, }, }; </script> 4. CSS code (you don’t need to read it, it is written only for code completeness, and can be run and displayed directly) <style scoped> .water-analysis { height: 100%; overflow:auto; } .content-box { height: 100%; } .tab-pane-box { height: calc(100% - 62px); } </style> ConclusionThis is the end of this article about how to batch import, register and use Vue components. For more relevant content about batch import of Vue components, 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:
|
<<: How to create a view on multiple tables in MySQL
1. Monitoring planning Before creating a monitori...
1. Demand We have three tables. We need to classi...
Table of contents 1. Simple mounting of persisten...
Unzip the Maven package tar xf apache-maven-3.5.4...
MySQL download and installation (version 8.0.20) ...
1. Download the ubuntu16.04 image and the corresp...
MySQL stored procedures, yes, look like very rare...
1. The Importance of Indexes Indexes are used to ...
Table of contents 1. Use the a tag to preview or ...
Table of contents 1. Introduction to High Availab...
The Flexbox layout module aims to provide a more ...
This article example shares the specific code of ...
Table of contents 1. Location Object 1. URL 2. Pr...
1. Brief introduction of the event An event is a ...
Building new images from existing images is done ...