Webpack导入变量undefined,除非我将其分配给另一个变量

时间:2018-05-18 01:12:45

标签: javascript reactjs webpack import

这有效:



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;
&#13;
&#13;

但这并不是:

&#13;
&#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;
&#13;
&#13;

错误:ReferenceError: register_ is not defined。作为参考,这就是ComponentsRegister.js的样子:

&#13;
&#13;
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;
&#13;
&#13;

它只是一个具有2个属性的对象数组:name和path。我将其创建为动态导入组件的方法。

0 个答案:

没有答案