data-center-components
Version:
84 lines (76 loc) • 2.83 kB
JavaScript
// // 导入button组件
// import SupconitAboutUs from './supconit-about-us'
// import SupconitPopup from './supconit-popup'
// import SupconitTable from './supconit-table'
// import SupconitCustomStatus from './supconit-custom-status'
// import SupconitIcon from './supconit-icon'
// import SupconitSwitchTabs from './supconit-switch-tabs'
// import SupconitBlockTitle from './supconit-block-title'
// // 组件列表
// const components = [
// SupconitAboutUs,
// SupconitPopup,
// SupconitTable,
// SupconitCustomStatus,
// SupconitIcon,
// SupconitSwitchTabs,
// SupconitBlockTitle
// ]
// // 定义 install 方法,接收 Vue 作为参数。如果使用 use 注册插件,那么所有的组件都会被注册
// const install = function (Vue) {
// if (install.installed) return
// // 遍历注册全局组件
// components.map(component => {
// if (component.name) {
// Vue.component(component.name, component)
// }else {
// // Vue.component(component.name, component)
// }
// }
// )
// }
// <--- 以上内容无效 --->
const requireComponent = require.context('./', true, /\.vue$/)
function capitalizeFirstLetter(str) {
return str?.slice(0)
}
/**
* 对符合'xx/xx.vue'组件格式的组件取组件名
* @param str fileName
* @example abc/bcd/def/basicTable.vue
* @return {string} BasicTable
*/
function validateFileName(str) {
return (
/^\S+\.vue$/.test(str) &&
str.replace(/^\S+\/(\w+)\.vue$/, (rs, $1) => capitalizeFirstLetter($1))
)
}
// // 定义 install 方法,接收 Vue 作为参数。如果使用 use 注册插件,那么所有的组件都会被注册
const install = function (app) {
// 找到组件文件夹下以.vue命名的文件,如果文件名为index,那么取组件中的name作为注册的组件名
requireComponent.keys().forEach(filePath => {
const componentConfig = requireComponent(filePath)
const fileName = validateFileName(filePath)
if(fileName) {
let componentName =
fileName.toLowerCase() === 'index'
? capitalizeFirstLetter(componentConfig.default.name)
: fileName
if (componentConfig.default.name || fileName.toLowerCase() === 'index') {
componentName = capitalizeFirstLetter(componentConfig.default.name)
}
if (!componentName) return
app.component(componentName, componentConfig.default || componentConfig)
}
})
}
// installNew()
// 判断是否是直接引入文件
if (typeof window !== 'undefined' && window.Vue) {
install(window.Vue)
}
export default {
// 导出的对象必须具有 install,才能被 Vue.use() 方法安装
install
}