index.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // import {
  2. // defineAsyncComponent
  3. // } from 'vue';
  4. // export default {
  5. // install(app) {
  6. // // const req = require.context('./', false, /\.vue$/) //获取当前文件夹下的所有.vue文件,并返回一个对象
  7. // const req =
  8. // import.meta.glob('./*.vue'); // //获取当前文件夹下的所有.vue文件,并返回一个对象
  9. // debugger
  10. // Object.keys(req).forEach(item => { // Object.keys遍历,返回一个包含所有属性名的数组,对数组遍历,根据属性名,通过obj[属性名]的方式拿取到属性值,相较于for in的优势在于只遍历了属性名,而非属性名和属性值全部遍历,性能有优化
  11. // debugger
  12. // const com = req(item).default //在default包裹层下拿到属性名
  13. // app.component(com.name, com)
  14. // })
  15. // }
  16. // }
  17. /*
  18. **全局注册组件
  19. */
  20. import {
  21. defineAsyncComponent
  22. } from 'vue';
  23. const components =import.meta.glob('./*.vue'); // 异步方式,获取当前文件夹下的所有.vue文件,并返回一个对象
  24. export default function install(app) {
  25. for (const [key, value] of Object.entries(components)) {
  26. const name = key.slice(key.lastIndexOf('/') + 1, key.lastIndexOf('.'));
  27. app.component(name, defineAsyncComponent(value));
  28. }
  29. }
  30. /*
  31. **全局注册组件
  32. */
  33. // import { defineAsyncComponent } from 'vue';
  34. // //获取components目录下所有的文件信息
  35. // const modulesFiles = import.meta.globEager('./*.vue')// 异步方式,获取当前文件夹下的所有.vue文件,并返回一个对象
  36. // const pathList = []
  37. // //遍历拿到所有的文件名称
  38. // for (const path in modulesFiles) {
  39. // pathList.push(path)
  40. // }
  41. // //全局批量注册components下所有组件
  42. // export default {
  43. // install(app) {
  44. // pathList.forEach((path) => {
  45. // debugger
  46. // const component = modulesFiles[path].default;
  47. // const name = path.slice(path.lastIndexOf('/') + 1, path.lastIndexOf('.'));
  48. // app.component(component.name, component)
  49. // // app.component(name, defineAsyncComponent(component))
  50. // })
  51. // }
  52. // }
  53. // const app = createApp(App);
  54. // function registerGlobalAsyncComponents(app: VueApp) {
  55. // const modules = import.meta.glob('./**/*.vue');
  56. // for (const path in modules) {
  57. // const result = path.match(/.*\/(.+).vue$/);
  58. // if (result) {
  59. // const name = result[1];
  60. // const component = modules[path];
  61. // app.component(name, defineAsyncComponent(component));
  62. // }
  63. // }
  64. // }
  65. // const modules = import.meta.glob('./components-new/*.vue');
  66. // const components: any = ref({});
  67. // Object.entries(modules).forEach(([path, asyncCom]) => {
  68. // const name = path.replace(/\.\/components-new\/(.*)\.vue/, '$1');
  69. // components.value[name] = markRaw(defineAsyncComponent(asyncCom));
  70. // });