ag-element-ui/packages/index.js

38 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 使用 require.context 自动扫描并获取 packages 目录下所有 index.js 文件的引用
const requireComponent = require.context('./', true, /index\.js$/);
// 定义一个数组来存储组件配置
const components = requireComponent.keys().reduce((components, fileName) => {
// 排除可能的非 Vue 组件文件(这里假设所有 index.js 都是 Vue 组件)
if (fileName.endsWith('index.js')) {
const componentConfig = requireComponent(fileName).default;
if (componentConfig && componentConfig.name) {
components[componentConfig.name] = componentConfig;
}
}
return components;
}, {});
// 定义 install 方法,接收 Vue 作为参数
const install = function (Vue) {
if (install.installed) return;
Object.keys(components).forEach(name => {
Vue.component(name, components[name]);
});
install.installed = true;
};
// 如果是在浏览器环境且全局有 Vue则自动安装
if (typeof window !== 'undefined' && window.Vue) {
install(window.Vue);
}
// 导出 install 方法和所有组件
export default {
// 为了方便单独引入组件,可以直接导出 components 对象
...components
};
// 注意:虽然这里导出了 components 对象,但通常不建议直接这样使用,
// 因为这样做会破坏组件的模块化封装。更常见的做法是
// 在需要时从该文件中单独导入特定的组件。