这有效:
import register from './ComponentsRegister';
const register_ = register; // Hacky
const Components = {}
for (var i=0; i<register_.length; i++) {
Components[register_[i].name] = require(''+register_[i].path).default;
}
export default Components;
&#13;
但这并不是:
import register_ from './ComponentsRegister';
// const register_ = register; // Hacky
const Components = {}
for (var i=0; i<register_.length; i++) {
Components[register_[i].name] = require(''+register_[i].path).default;
}
export default Components;
&#13;
错误:ReferenceError: register_ is not defined
。作为参考,这就是ComponentsRegister.js的样子:
const register = [
{
name: 'About',
path: './components/About'
},
{
name: 'Footer',
path: './components/Footer'
},
{
name: 'Header',
path: './components/Header'
},
{
name: 'Home',
path: './components/Home'
}
];
export default register;
&#13;
它只是一个具有2个属性的对象数组:name和path。我将其创建为动态导入组件的方法。