require.context()
参数: 1、文件路径;
2、是否匹配子级文件(true,false);
3、正则,匹配的规则
demo:
const requireComponent = require.context('.', false, /\.vue$/); const install = (Vue) => { if (install.installed) return; install.installed; requireComponent.keys().forEach(fileName => { const config = requireComponent(fileName); const componentName = config.default.name; Vue.component(componentName, config.default || config); }) } if (typeof window !== 'undefined' && window.Vue) { install(window.Vue); } export default { install }