SoFunction
Updated on 2025-04-05

Operation code for batch introduction of components of vue page

<template>
  <div>
    <template v-for="(item) in names">
      <component :is="item" :key="item" />
    </template>
  </div>
</template>
<script>
// It's feasibleimport path from 'path'

// (param1,param2,param3) param1:Path; param2: Whether to search for subfolders; param3: file type, can be regularconst files = ('@/components/Menu/Dialog', true, /\.vue$/)

const dialogs = {}
const names = []
// Component import().forEach((key) => {
   /**
     *
     * Get the vue file name
     * Method 1: Use regular expressions to match
     * (/^\.\/(.*)\.\w+$/, '$1')
     * Method 1: Get the vue file name
     * import path from 'path'
     * (path[, ext])
	 The * () method returns the last part of path.  The directory separator at the tail is ignored.
     **/
    
  // Get file name Method 1  // var name = fileName
  //  .split('/')
  //  .pop()
  //  .replace(/\.\w+$/, '');

  // Get file name Method 2  const name = (key, '.vue')
  
  (name)
  dialogs[name] = files(key).default || files(key)
})

export default {
  name: 'Dialogs',
  components: dialogs,
  data() {
    return {
      names: names
    }
  }
}
</script>

<style lang="scss" scoped />

Knowledge points:

(param1,param2,param3)
  • param1:path;
  • param2: Whether to search for subfolders;
  • param3: file type, regular
(path[, ext])

The method returns the last part of path. The directory separator at the tail will be ignored

  • path :string
  • ext : string optional file extension.
('/directory 1/directory 2/file.html');  // document.html
('/directory 1/directory 2/file.html', '.html');  // document

This is the end of this article about batch introduction of components on vue pages. For more related content on batch introduction of components on vue pages, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!