简体   繁体   English

ERROR ReferenceError: Q is not defined in Angular 11 project

[英]ERROR ReferenceError: Q is not defined in Angular 11 project

I have added a custom javascript file in my Angular 11 project (myCustomJS.js) that utilizes Q library.我在使用 Q 库的 Angular 11 项目 (myCustomJS.js) 中添加了一个自定义 javascript 文件。

However, I am getting "ERROR ReferenceError: Q is not defined" at below line of code in my javascript file.但是,我在 javascript 文件中的以下代码行中收到“错误 ReferenceError:Q 未定义”。

Error : "ERROR ReferenceError: Q is not defined"错误:“错误参考错误:未定义 Q”

On Line : var deferred = Q.defer();在线: var deferred = Q.defer();

FYI, I have added following packages in package.config:仅供参考,我在 package.config 中添加了以下软件包:

{
    "@types/q": "^1.5.4",
    "q": "^1.5.1",
}

I have added following in angular.json file:我在 angular.json 文件中添加了以下内容:

"scripts": [
    "node_modules/q/q.js",  // Added reference to Q Library
    "src/assets/js/myCustomJS.js"   // My Custom Javascript File
]

Added following types in tsconfig.app.json file:在 tsconfig.app.json 文件中添加了以下类型:

"types": [
    "jquery",
    "q" // Added q type definitions
]

Not sure if I am missing any more reference(s) in my Angular 11 project.不确定我的 Angular 11 项目中是否缺少更多参考。 Please suggest how to fix Q is not defined error.请建议如何修复 Q 未定义错误。

I was able to resolve “Q not defined” issue by commenting 2 lines (Line# 46 and 47 - else if block) in /node_modules/q/q.js file (see below screenshot).我能够通过在 /node_modules/q/q.js 文件中注释 2 行(第 46 行和第 47 行 - else if 块)来解决“Q 未定义”问题(见下面的屏幕截图)。 Due to those 2 lines, it was not able to define Q as global variable, as specially when it finds RequireJS.由于这两行,它无法将 Q 定义为全局变量,特别是当它找到 RequireJS 时。 Once commented, it falls into another else if block where it defines Q global variable.一旦被注释,它就会落入另一个定义 Q 全局变量的 else if 块中。

注释 RequireJS else-if 块

Here are couple of articles and different solutions related to this issue:以下是与此问题相关的几篇文章和不同的解决方案:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM