我正在尝试将我的Angular 9应用程序升级到Angular 10版本,但在升级后我收到了下面的警告
rxjs\BehaviorSubject.js depends on rxjs-compat/BehaviorSubject
我该如何修复这个问题?
我正在尝试将我的Angular 9应用程序升级到Angular 10版本,但在升级后我收到了下面的警告
rxjs\BehaviorSubject.js depends on rxjs-compat/BehaviorSubject
我该如何修复这个问题?
当你使用 CommonJS 打包的依赖时,会导致应用程序变得更大、更慢, 详情请见
从版本 10 开始,Angular 会在构建过程中提示你是否引用了这些包。如果你看到了这些警告,请告诉相关依赖项,你希望使用 ECMAScript 模块(ESM)打包。
以下是官方文档 - 配置 CommonJS 依赖项
在你的 angular.json 文件中查找 build 对象并添加
allowedCommonJsDependencies
如下所示 -
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"allowedCommonJsDependencies": [
"rxjs-compat",
... few more commonjs dependencies ...
]
...
}
...
},
angular.json
。 - Maxim Georgievskiy尝试将rxjs导入中的rxjs/internal/operators
替换为rxjs/operators
。
示例:
import { catchError, retry } from 'rxjs/internal/operators';
随着
import { catchError, retry } from 'rxjs/operators';
建议您在 Angular 应用程序中避免使用 CommonJS 模块。依赖于 CommonJS 模块可能会阻止捆绑和最小化器优化您的应用程序,导致包大小变大。相反,建议您在整个应用程序中使用 ECMAScript 模块。
然而,如果您不关心打包大小,并且希望禁用这些警告,则可以将 CommonJS 模块名称添加到位于 angular.json 文件中的构建选项的 allowedCommonJsDependencies
选项中。
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"allowedCommonJsDependencies": [
"rxjs-compat"
]
...
}
...
},
对于RXJS库,您可以进行以下更改。
对于导入,例如'rxjs/internal/<anything>'
和'rxjs/index'
,请用'rxjs'
替换它们。
对于像'rxjs/internal/operators'
这样的导入,请使用'rxjs/operators'
替换它们。
或仅替换rxjs
。
只需要更改导入方式:
从:
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
到:
import { BehaviorSubject } from 'rxjs';
BehaviorSubject
的方式:
https://www.learnrxjs.io/learn-rxjs/subjects/behaviorsubject - Adel Kedjour{
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"allowedCommonJsDependencies": [
"rxjs"
]
}
}
}
}
我曾经遇到过类似的问题(app.module.ts 依赖于 'ngx-google-places-autocomplete'),但是很多答案都没有帮助到我。
所以,如果你的 x 依赖于 y,只需在 "allowedCommonJsDependencies" 中的 angular.json 文件中添加 y。
flatMap
已被弃用(从rxjs/operators
中)。这是mergeMap
的别名,所以我只需将以下内容替换即可:
import { flatMap } from 'rxjs/internal/operators';
替换为
import { mergeMap } from 'rxjs/operators';
import { BehaviorSubject } from 'rxjs'
,对于 Angular 9 一切正常。但对于 Angular 10 不行。我在使用 map 操作符时也遇到了同样的问题 - 它显示 WARNING in ..\node_modules\rxjs\operators\map.js depends on rxjs-compat/operators/map. CommonJS 或 AMD 依赖项可能会导致优化失败。 - Ora