Batch introduction of vue instructions

like components , create a directives folder in the project directory to store instruction files;
an instruction is a file, and the introduction of main.js files can achieve global introduction;
although many cases are introduced on demand, but want to know how to batch global introduction?

Mar.23,2021

batch global import instructions, which can be registered through forEach traversing Vue.directive

import * as directives from './directives'
// 
Object.keys(directives).forEach(k => Vue.directive(k, directives[k]))

this article can help you understand global instruction wrapper
https://codeshelper.com/a/11.


import Vue from 'vue'

// https://webpack.js.org/guides/dependency-management/-sharprequire-context
const requireDirective = require.context(
  // 
  './directives',
  // 
  false,
  // js
  /.+\.js$/
)

// 
requireDirective.keys().forEach(fileName => {
  // 
  const directiveConfig = requireDirective(fileName)

  const directiveName = fileName
        //  './'
        .replace(/^\.\//, '')
        // 
        .replace(/\.\w+$/, '');
 
  // , 
  Vue.directive(directiveName, directiveConfig.default || directiveConfig)
})
Menu