2016-05-03 37 views
13

我使用的是服务于我的angular应用程序中创建uibModal如下严重依赖 - 依赖的请求是表达的WebPack

function modal(modalConfig){ 
        var modalInstance = $uibModal.open({ 
        animation: true, 
        template: require("../a/b/xyz.html"), 
        controller: modalConfig.controller, 
        size: modalConfig.size, 
        controllerAs: modalConfig.controllerAs, 
        bindToController : true, 
        resolve: modalConfig.resolveObj 

       }); 
      } 

请注意行

template: require("../a/b/xyz.html"), 

我想在这样的地方使用变量

template: require(modalConfig.templateUrl), 

但当我代替硬编码值使用变量webpack给我

Critical dependencies: 
83:22-54 the request of a dependency is an expression 

我不能够解决这个错误。可能的原因是什么?我使用node-express服务器连续webpack构建。我也查看了其他答案,但他们没有解决我的问题。

回答

14

经过多次打击,审判中发现的solution.What我这样做是:

template: require("../../scripts" + modalConfig.templateUrl + ".html") 

假设下,所有的文件来是scripts

  • 和相对路径

    1. 根文件夹来自写入该函数的文件的该文件夹的名称是../../scripts
    2. ../../scripts + modalConfig.templateUrl + ".html"将形成要使用的文件的正确路径。

    强制性注

    1. 总是写根文件夹的一些硬编码路径。不要把它放在变量中。所以这是行不通的

      var context = "../../scripts" ; template: require(context + modalConfig.templateUrl + ".html")

    的基本路径(如实际路径的一部分)已被硬编码为基准,在其帮助的WebPack创建所有列表动态需要的模块可能需要。

    原因(来自webpack docs),读取dynamic requirescontext module

  • +1

    为我节省了大量的时间。谢谢! –