平时写项目 如果对一个页面有很多种组件要进行引入,一般都是这样引入
这无异于造成一大堆屎山,(但是秉着我的代码别人改不了,老板只能加工资的原则),引入一种新型注册方式
require.context
详细的教程可以百度,这边教你怎么用
这是我的组件目录
require.context()主要是三个参数(文件夹路径,是否查询子目录,什么结尾的文件)
直接上代码
const components={}
console.log(component.keys())
component.keys().forEach(path => {
const name = path.split('/')[2].replace(/\.vue$/,'')
const componentConfig=component(path)
components[name]=componentConfig.default||componentConfig
})
console.log(components)bash
在这里插入代码片
在layout的路径的文件每个文件名都要设置name
这样name就是子组件的名字了