[英]Uncaught TypeError: __webpack_require__(...).context is not a function
When I try to dynamically import xml files using this code:当我尝试使用此代码动态导入 xml 文件时:
const path = require.context(`../report/${runId}/`, false, /\.xml$/);
I am getting this error:我收到此错误:
Uncaught (in promise) TypeError: __webpack_require__(...).context is not a function
at fetchData (xmlparser.js:28:1)
at Run.getTests (Run.js:16:1)
at Run.componentDidMount (Run.js:10:1)
at commitLayoutEffectOnFiber (react-dom.development.js:23305:1)
at commitLayoutMountEffects_complete (react-dom.development.js:24688:1)
at commitLayoutEffects_begin (react-dom.development.js:24674:1)
at commitLayoutEffects (react-dom.development.js:24612:1)
at commitRootImpl (react-dom.development.js:26823:1)
at commitRoot (react-dom.development.js:26682:1)
at performSyncWorkOnRoot (react-dom.development.js:26117:1)
But as soon as I import the XML files as following it works:但是一旦我导入 XML 文件,它就可以工作了:
const path = require.context(`../report/1/`, false, /\.xml$/);
Note - runId is declared as 1, so it isn't undefined注意 - runId 被声明为 1,所以它不是未定义的
--edit : --编辑:
my answer is wrong.我的回答是错误的。 stackoverflow.com/questions/54059179/what-is-require-context require.context is evaluated at compile time, so you must pass a static string, not a variable or dynamic concatenation, evaluated runtime. stackoverflow.com/questions/54059179/what-is-require-context require.context 在编译时评估,因此您必须传递一个 static 字符串,而不是变量或动态连接,评估运行时。 This is why the second example works.这就是第二个示例有效的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.