【问题标题】:Angular + amazon-cognito-identity-js, why am I getting error: global is not definedAngular + amazon-cognito-identity-js,为什么我收到错误:未定义全局
【发布时间】:2019-02-10 00:48:02
【问题描述】:

从命令行:

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)

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

【问题讨论】:

    标签: angular typescript amazon-cognito


    【解决方案1】:

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

    (window as any).global = window
    

    【讨论】:

    • 非常感谢!这是为我做的! :) 我们可以知道问题是什么以及这条线是如何解决的吗?谢谢:D
    • @Mon 你有没有弄清楚为什么这条线有效?
    • 不,很遗憾。最近也没有做任何前端。希望@danday74 能回答?
    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 2019-12-23
    • 2020-09-28
    • 2012-01-30
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多