例如,在form文件夹下有许多流程类表单,一个一个注册引入非常浪费时间,可使用webpack的require.context方法来自动导入
import { firstToUpper } from '@/utils/methods'const taskTable = {install: vue => {const req = require.context('../form', true, /\.vue/)req.keys().forEach(item => {let moduleName = item.match(/\/(\S*)\./)[1]moduleName = moduleName.split('/')moduleName = firstToUpper(moduleName[0]) + moduleName[1]const module = req(item).defaultvue.component(moduleName, module)})}
}
export default taskTable
然后在main.js中调用
Vue.use(taskTable)