typescript Angular + amazon-cognito-identity-js,为什么我会得到错误:未定义全局

yhxst69z  于 2023-01-10  发布在  TypeScript
关注(0)|答案(2)|浏览(127)

从命令行:

ng new sandbox
cd .\sandbox\
ng serve

打开http://localhost:4200/,它工作了。

npm install --save amazon-cognito-identity-js

\src\app\sign-up\sign-up.component.ts中添加以下代码行:

import * as AmazonCognitoIdentity from 'amazon-cognito-identity-js';

添加构造函数:

constructor() {
    new AmazonCognitoIdentity.CognitoUserPool({});
  }

刷新http://localhost:4200/
页面为空。出现控制台错误:

Uncaught ReferenceError: global is not defined
    at Object../node_modules/buffer/index.js (index.js:43)
    at __webpack_require__ (bootstrap:76)
    at Object../node_modules/amazon-cognito-identity-js/es/AuthenticationHelper.js (AuthenticationHelper.js:1)
    at __webpack_require__ (bootstrap:76)
    at Object../node_modules/amazon-cognito-identity-js/es/index.js (index.js:1)
    at __webpack_require__ (bootstrap:76)
    at Object../src/app/app.component.ts (main.js:94)
    at __webpack_require__ (bootstrap:76)
    at Object../src/app/app.module.ts (app.component.ts:9)
    at __webpack_require__ (bootstrap:76)

解决此错误的最佳方法是什么?

kx5bkwkv

kx5bkwkv1#

将此作为/src/polyfills.ts中的最后一行

(window as any).global = window
myzjeezk

myzjeezk2#

我一直在寻找这个问题,我能够修复以下链接:https://github.com/aws-amplify/amplify-js/issues/678#issuecomment-384260863
if(全局===未定义){ var全局=窗口;}

相关问题