我正在升级一个现有的Angular应用到Angular 15。我在浏览器中收到以下错误:
index.js:43 Uncaught ReferenceError: global is not defined
at 34768 (index.js:43:30)
at __webpack_require__ (bootstrap:19:1)
at 92634 (AuthenticationDetails.js:65:4)
at __webpack_require__ (bootstrap:19:1)
at 53843 (UserAgent.js:19:25)
at __webpack_require__ (bootstrap:19:1)
at 48562 (vendor.js:26:84)
at __webpack_require__ (bootstrap:19:1)
at 88951 (auth.module.ts:8:25)
at __webpack_require__ (bootstrap:19:1)
这个问题过去是通过在polyfills.ts
中添加以下代码来解决的。(window as any).global = window;
我相信这个文件已经从Angular 15中删除了。我现在如何解决这个错误?
我试着创建一个多边形。没有任何成功
根据这个PR,你应该把它放在一个文件中,并在你的angular中指向它。在polyfills
数组下使用。例子:
// other stuff in the angular.json
polyfills: [ "./window-global-fix.ts" ],
当我遇到这个问题时,我正在使用AWS Amplify根据可用的在线教程配置Angular 15。上面提供的答案最终揭示了我需要做什么,所以我给上面的答案点了赞。我把这个放在这里是为了给任何遇到类似Angular15+Amplify问题的人提供快速参考。
1)。创建某种文件来代替" polyfill .ts">
Angular-Project/src/main/webapp/src/js - polyfill -like-file.ts
(window as any).global = window;
(window as any).process = {
env: { DEBUG: undefined },
};
2。)添加到tsconfig.app.json中Angular-Project/…/webapp/tsconfig.app.json
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
...
},
"files": [
"src/aws-pollyfill-like-file.ts",
"src/main.ts"
],
...
}
3)。添加到angular.json中Angular-Project/…/webapp/angular.json
{
"$schema": ...,
...
"projects": {
"angularclient": {
...
"architect": {
"build": {
...
"options": {
...
"polyfills": [
"zone.js",
"src/aws-pollyfill-like-file.ts"
],
...
},
...
},
}
}
}
}
更简单的方法是包含
<script>
if (global === undefined) {
var global = window;
}
</script>
<head>