自动注册components下的组件,类似uni-app的easycom
import Vue from 'vue';function capitalizeFirstLetter(string){return string.charAt(0).toUpperCase() + string.slice(1);}const requireComponent = require.context('./components',true,/\.vue$///找到component
·
import Vue from 'vue';
function capitalizeFirstLetter(string){
return string.charAt(0).toUpperCase() + string.slice(1);
}
const requireComponent = require.context(
'./components',true,/\.vue$/
//找到components文件夹下以.vue命名的文件
)
requireComponent.keys().forEach(fileName => {
const componetConfig = requireComponent(fileName)
let a = fileName.lastIndexOf('/');
fileName = '.' + fileName.slice(a);
const componetName = capitalizeFirstLetter(
fileName.replace(/^\.\//,'').replace(/\.\w+$/,'')
)
Vue.component(componetConfig.default.name || componetName, componetConfig.default || componetConfig)
})
更多推荐
已为社区贡献1条内容
所有评论(0)