2016-01-06 20 views
9

我想要一个文件。它的路径是在一些配置:同步需要动态路径

//config.js 
module.exports = "/home/css/style.css"; 

//entry.js 
var path = require(./config.js); 
require("style!css!" + path); 

Uncaught Error: Cannot find module "." 

我怎么能要求与路径文件,可以扎根/目录?

回答

1

你将不得不使用require.ensure
对于前:

function handleRouteChange(path) { 
    require(['/views/' + path], function(PageView) { 
     app.setView(new PageView()); 
    }); 
} 

变化:

function loadPage(PageView) { 
    app.setView(new PageView()); 
} 
function handleRouteChange(path) { 
    switch (path) { 
     case 'settings': 
      require(['/views/settings'], loadPage); 
      break; 
     case 'transfer': 
      require(['/views/transfer'], loadPage); 
      break; 
     case 'wallet': 
      require(['/views/wallet'], loadPage); 
      break; 
    default: 
     // you could either require everything here as a last resort or just leave it... 
    } 
} 

来源:https://gist.github.com/xjamundx/b1c800e9282e16a6a18e#the-routing-code-old-and-new

+0

我想设置的绝对路径在我的Linux系统中,webpack通过整个系统太多了h一些装载器(我的例子中的样式和css) – mqklin

+0

你的意思是说一些webpack可以把你的根目录放在哪里? –

+0

是的。也许有些插件允许这样做。 – mqklin