injectModule.js 755 B

123456789101112131415161718
  1. export const registerGlobalAsyncComponents = (Vue) => {
  2. const context = require.context('@/template', true, /Sly[A-Za-z]+\.vue$/);
  3. context.keys().forEach(path => {
  4. const [_, name, file] = path.split('/'); // 根据路径获取组件名称
  5. if (name.indexOf('Sly') === 0) {
  6. const fileName = file ? file.split('.vue')[0] : '';
  7. const component = context(path)
  8. const ctrl = component.default || component
  9. // console.log("file.split('.vue') =" , ctrl.name ,file.split('.vue'))
  10. const a = ctrl.name || fileName || name // 如组件内部未定义组件名称,则取文件夹名称为组件名称
  11. const b = ctrl
  12. if (a === name) {
  13. // 注册组件(和文件夹同名才注册,类似于index)
  14. Vue.component(a, b)
  15. }
  16. }
  17. })
  18. }