2017-02-27 74 views
0

我很新的angularjs。我正在用browserify開發應用程序。需要角度與browserify

當我用require只是字符串,它的工作。但是當我將它與一些組合的字符串一起使用時,它不起作用。

我的意思是require('./todo')正在工作,但var todo = 'todo'; require('./' + todo)不是。

問題代碼index.js在這裏。

var fs = require('fs'); 

    var files = fs.readdirSync(__dirname); 
    var controllers = module.exports = {}; 

    // files.length is 2 and the names are index.js and todo.js 
    for (var i=0; i<files.length; i++) { 
    var file = files[i]; 
    if (file !== 'index.js') { 
     var fileName = file.split('.')[0]; 
     var modulePath = './' + fileName; 

     // below code is working. 
     controllers[fileName] = require('./todo'); 

     // below code is not working. The error code -> Uncaught Error: Cannot find module './todo' 
     controllers[fileName] = require(modulePath); 
    } 
    } 

什麼問題?

回答

0

我想出了這樣一個事實,即browserify不允許'需要'的Concat,比如require(「./ fi」+「le」)。但另一個捆綁軟件如webpack則允許使用該功能。