12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- // import {
- // defineAsyncComponent
- // } from 'vue';
- // export default {
- // install(app) {
- // // const req = require.context('./', false, /\.vue$/) //获取当前文件夹下的所有.vue文件,并返回一个对象
- // const req =
- // import.meta.glob('./*.vue'); // //获取当前文件夹下的所有.vue文件,并返回一个对象
- // debugger
- // Object.keys(req).forEach(item => { // Object.keys遍历,返回一个包含所有属性名的数组,对数组遍历,根据属性名,通过obj[属性名]的方式拿取到属性值,相较于for in的优势在于只遍历了属性名,而非属性名和属性值全部遍历,性能有优化
- // debugger
- // const com = req(item).default //在default包裹层下拿到属性名
- // app.component(com.name, com)
- // })
- // }
- // }
- /*
- **全局注册组件
- */
- import {
- defineAsyncComponent
- } from 'vue';
- const components =import.meta.glob('./*.vue'); // 异步方式,获取当前文件夹下的所有.vue文件,并返回一个对象
- export default function install(app) {
- for (const [key, value] of Object.entries(components)) {
- const name = key.slice(key.lastIndexOf('/') + 1, key.lastIndexOf('.'));
- app.component(name, defineAsyncComponent(value));
- }
- }
- /*
- **全局注册组件
- */
- // import { defineAsyncComponent } from 'vue';
- // //获取components目录下所有的文件信息
- // const modulesFiles = import.meta.globEager('./*.vue')// 异步方式,获取当前文件夹下的所有.vue文件,并返回一个对象
- // const pathList = []
- // //遍历拿到所有的文件名称
- // for (const path in modulesFiles) {
- // pathList.push(path)
- // }
- // //全局批量注册components下所有组件
- // export default {
- // install(app) {
- // pathList.forEach((path) => {
- // debugger
- // const component = modulesFiles[path].default;
- // const name = path.slice(path.lastIndexOf('/') + 1, path.lastIndexOf('.'));
- // app.component(component.name, component)
- // // app.component(name, defineAsyncComponent(component))
- // })
- // }
- // }
- // const app = createApp(App);
- // function registerGlobalAsyncComponents(app: VueApp) {
- // const modules = import.meta.glob('./**/*.vue');
- // for (const path in modules) {
- // const result = path.match(/.*\/(.+).vue$/);
- // if (result) {
- // const name = result[1];
- // const component = modules[path];
- // app.component(name, defineAsyncComponent(component));
- // }
- // }
- // }
- // const modules = import.meta.glob('./components-new/*.vue');
- // const components: any = ref({});
- // Object.entries(modules).forEach(([path, asyncCom]) => {
- // const name = path.replace(/\.\/components-new\/(.*)\.vue/, '$1');
- // components.value[name] = markRaw(defineAsyncComponent(asyncCom));
- // });
|