React

时间:2017-01-19 16:34:12

标签: javascript node.js reactjs

在运行反应时,我似乎对此脚本存在范围问题,我正在尝试创建一个变量,该变量在指定文件夹中的每个文件的路径中都包含一个字符串。这段代码在节点控制台上运行得很好,但是这些变量在反应环境中使用时似乎给了我error 'imgChar' is not defined

const fs = require('fs');
function importFiles(filepath, prefix) {
  fs.readdir(filepath, (err, files) => {
    files.forEach(file => {
      var doc = file
      var char = /.+(?=\.\w+)/.exec(file);
      if (char !== null) {
        eval(prefix + char + " = " + "'" + String(filepath + doc) + "'");
      }
    });
  })
}
importFiles('../public/img/', 'img');

P.S。我也意识到eval()可能不是一种智能和安全的方式来实现这一点,所以任何关于更智能实现的建议都将不胜感激!

0 个答案:

没有答案