关键依赖 - 依赖的请求是一个表达式 Webpack [英] Critical dependencies - the request of a dependency is an expression Webpack
问题描述
我在我的 angular
应用程序中使用一个服务来创建 uibModal
如下
I am using a service in my angular
application to create uibModal
as follows
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"),
我想像这样在它的位置使用一个变量
I want to use a variable in its place like this
template: require(modalConfig.templateUrl),
但是当我使用变量代替硬编码值时 webpack
给了我
but when i use a variable in place of hard coded value webpack
gives me
Critical dependencies:
83:22-54 the request of a dependency is an expression
我无法解决此错误.可能的原因是什么?
I am not able to resolve this error. What can be the possible reason for it?
我使用 node-express
服务器进行连续的 webpack
构建.我也看过其他答案,但他们没有解决我的问题.
I have used node-express
server for continuous webpack
builds. I have looked at other answers too but they didn't solve my query.
推荐答案
经过多次尝试和尝试找到了解决方案.我所做的是:
After much hit and trial found the solution.What i did is this:
template: require("../../scripts" + modalConfig.templateUrl + ".html")
假设
- 所有文件所在的根目录是
scripts
- 并且该文件夹在编写函数的文件中的相对路径是
../../scripts
. ../../scripts
+modalConfig.templateUrl
+".html"
将形成文件的正确路径使用.
- root folder under which all the file comes is
scripts
- and the relative path of this folder from the file in which the function is written is say
../../scripts
. ../../scripts
+modalConfig.templateUrl
+".html"
will form the correct path for the file to be used.
强制性说明
总是写一些根文件夹的硬编码路径.不要把它放在变量中.所以这行不通
Always write some hardcoded path of root folder. Don't put it in variable. so this won't work
var context = "../../scripts" ;模板:需要(上下文 + modalConfig.templateUrl + ".html")
基本路径(作为实际路径的一部分)必须硬编码以供基本参考,因为它有助于 webpack 创建动态需要可能需要的所有模块的列表.
The base path (as in a part of the actual path) has to be hardcoded for basic reference, as in it helps webpack to create a list of all the modules which might be needed for the dynamic requires.
原因(来自 webpack docs),阅读
Reason (from webpack docs) , read dynamic requires.
这篇关于关键依赖 - 依赖的请求是一个表达式 Webpack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!