[英]Typescript Reference error is not defined, webpack angular2
This is not a general not defined error, I've been trying to debug it for the past two hours. 这不是一般性的未定义错误,在过去的两个小时中,我一直在尝试对其进行调试。
I am defining a value in webpack plugins (for a global variable) for API endpoint and trying to access it in the application gives a weird error. 我在webpack插件(用于全局变量)中为API端点定义一个值,并尝试在应用程序中访问它给出了一个奇怪的错误。 So here is the plugins structure in
webpack.dev.js
(which merges into webpack.config.js). 这是
webpack.dev.js
的插件结构(合并到webpack.config.js中)。
new DefinePlugin({
'ENV': JSON.stringify(METADATA.ENV),
'HMR': METADATA.HMR,
// ... some other lines
'API_PARENT': "DEV_PARENT_TEST" // this is the line in question
})
in custom-typings.d.ts
I have declared it to avoid ts erros 在
custom-typings.d.ts
我声明了它以避免ts erros
declare var API_PARENT: string;
Now in one of my app components when I try console.log(API_PARENT)
I get the mysterious error bellow 现在当我尝试使用
console.log(API_PARENT)
时,在我的一个应用程序组件中,我得到了下面的神秘错误
EXCEPTION: Uncaught (in promise): ReferenceError: DEV_PARENT_TEST is not defined
ReferenceError: DEV_PARENT_TEST is not defined
The stack trace leads to that log line. 堆栈跟踪通向该日志行。 The part I don't get is that why this is throwing up in the first place.
我没有得到的部分是为什么它首先被抛出。 the
DEV_PARENT_TEST
is a value not even a key why is there a reference error on it! DEV_PARENT_TEST
是一个值,甚至不是键,为什么会出现引用错误!
I am answering this just in case someone else faced this error. 我正在回答这个问题,以防万一其他人遇到此错误。 It wasted 3 hours of my time until I solve it.
我浪费了3个小时的时间,直到我解决了。 The clue was in the first line
JSON.stringify
. 线索在第一行
JSON.stringify
。
You must do that for all your string values. 您必须对所有字符串值执行此操作。 So the only change is in
webpack.dev.js
which should be 所以唯一的变化是在
webpack.dev.js
中
'API_PARENT': JSON.stringify("DEV_PARENT_TEST")
That that solves it all. 那解决了所有问题。 Most likely this was a webpack question.
这很可能是一个webpack问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.