node.js - TS1086:在使用 docker-compose 的 Angular 8 应用中,不能在声明环境上下文中声明访问器
我在本地的 Angular 8 应用中运行“ng serve”,一切正常。但是当我使用 Docker 和 Docker Compose 启动应用时,在 @zxing/ngx-scanner 上出现“在环境上下文中不能声明访问器”的错误
| ERROR in ../node_modules/@zxing/ngx-scanner/lib/browser-multi-format-continuous-reader.d.ts:12:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 12 get isTorchAvailable(): Observable;
| ~~~~~~~~~~~~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:104:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 104 get codeReader(): BrowserMultiFormatContinuousReader;
| ~~~~~~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:108:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 108 set device(device: MediaDeviceInfo | null);
| ~~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:116:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 116 get device(): MediaDeviceInfo | null;
| ~~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:120:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 120 get formats(): BarcodeFormat[];
| ~~~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:126:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 126 set formats(input: BarcodeFormat[]);
| ~~~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:130:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 130 get hints(): Map<DecodeHintType, any>;
| ~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:134:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 134 set hints(hints: Map<DecodeHintType, any>);
| ~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:138:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 138 set isAutostarting(state: boolean | null);
| ~~~~~~~~~~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:142:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 142 get isAutostarting(): boolean | null;
| ~~~~~~~~~~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:146:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 146 set torch(on: boolean);
| ~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:150:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 150 set enable(enabled: boolean);
| ~~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:154:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 154 get enabled(): boolean;
| ~~~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:158:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 158 get tryHarder(): boolean;
| ~~~~~~~~~
| ../node_modules/@zxing/ngx-scanner/lib/zxing-scanner.component.d.ts:162:9 - error TS1086: An accessor cannot be declared in an ambient context.
|
| 162 set tryHarder(enable: boolean);
| ~~~~~~~~~
|
| ** Angular Live Development Server is listening on 0.0.0.0:4200, open your browser on http://localhost:4200/ **
| ℹ 「wdm」: Failed to compile.
有人知道原因吗?我在修好它之前无法测试我的应用程序。
在我的 Angular 8 应用中启用 PWA 后,在 docker-compose 构建时遇到问题。通常当我执行 "ng serve" 时,编译会成功且没有任何错误。
Package.json:
"@angular/animations": "^8.2.14",
"@angular/cdk": "^8.2.3",
"@angular/common": "^8.2.14",
"@angular/compiler": "^8.2.14",
"@angular/core": "^8.2.14",
"@angular/flex-layout": "^7.0.0-beta.23",
"@angular/forms": "^8.2.14",
"@angular/material": "^7.3.2",
"@angular/platform-browser": "^8.2.14",
"@angular/platform-browser-dynamic": "^8.2.14",
"@angular/router": "^8.2.14",
"@zxing/ngx-scanner": "3.0.0"
我找到了一些在 tsconfig.json 中添加 skipLibCheck 的解决方案,但我不想跳过库检查。
"angularCompilerOptions": {
"skipLibCheck": true
}
有人能帮我解决这个问题吗。提前谢谢。
答案#1
我能看到你正在使用 Angular 8 版本。ngx-scanner 的最新版本不支持 Angular 8。
有两种解决方法:
- 将 Angular 更新到版本 12 以使用最新的 ngx-scanner 版本。
- 将 ngx-scanner 降级到 ~2.0.1
答案#2
在尝试启用 PWA 时,我遗漏了一些配置,我能够找到问题并已修复。同时也进行了测试,问题并不是由于 @zxing/ngx-scanner 引起的
感谢每一位花时间找出问题根本原因的人。