Angular + amazon-cognito-identity-js, why am I getting error: global is not defined

感情迁移 提交于 2019-12-13 01:43:54

问题


From a command line:

ng new sandbox
cd .\sandbox\
ng serve

Open http://localhost:4200/. It works.

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

Inside \src\app\sign-up\sign-up.component.ts add this line of code:

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

Add a constructor:

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

Refresh http://localhost:4200/.

Page is blank. There is a console error:

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)

What is the best way to resolve this error?


回答1:


Put this as the last line in /src/polyfills.ts

(window as any).global = window


来源:https://stackoverflow.com/questions/52175544/angular-amazon-cognito-identity-js-why-am-i-getting-error-global-is-not-defi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!