Zone.js 检测到 ZoneAwarePromise `(window|global).Promise` 已被覆盖

use*_*866 10 javascript node.js typescript ecmascript-6 angular

我正在尝试在我的应用程序中使用 Typeform 库,但是我遇到了很多问题。加载 js 脚本后,Angular 区域错误,我收到此消息:

错误:Zone.js 检测到 ZoneAwarePromise(window|global).Promise已被覆盖。最可能的原因是在 Zone.js 之后加载了 Promise polyfill(加载 zone.js 时不需要 Polyfilling Promise api。如果必须加载,请在加载 zone.js 之前加载。)

我的 app.component.ts 的代码是:

import { Component, Inject, AfterViewInit} from '@angular/core';
import { DOCUMENT } from '@angular/common';
import * as typeformEmbed from '@typeform/embed';

@Component({
  selector: 'my-app',
  template: `<div #my_typeform></div>`,
})


export class AppComponent implements AfterViewInit {
  constructor(
    @Inject(DOCUMENT) private document: Document
  ){}

  ngAfterViewInit() {
    const element = this.document.getElementById.call(document, 'my_typeform');
    typeformEmbed.makeWidget(element, 'https://jonathan.typeform.com/to/zvlr4L', { onSubmit: () => console.log('Close') });
  }
}

Run Code Online (Sandbox Code Playgroud)

我尝试手动运行 ngZone 以确保它在 Angular zone 内运行,但没有奏效。在这种情况下, app.component.ts 就像

import { Component, Inject, AfterViewInit, NgZone} from '@angular/core';
import { DOCUMENT } from '@angular/common';
import * as typeformEmbed from '@typeform/embed';

@Component({
  selector: 'my-app',
  template: `<div #my_typeform></div>`,
})


export class AppComponent implements AfterViewInit {
  constructor(
    @Inject(DOCUMENT) private document: any,
    @Inject(NgZone) private ngZone: NgZone
  ){}

  ngAfterViewInit() {
    this.ngZone.run(() => {
      const element = this.document.getElementById.call(document, 'my_typeform');
      typeformEmbed.makeWidget(element, 'https://jonathan.typeform.com/to/zvlr4L', { onSubmit: () => console.log('Close') });
    });
  }
}

Run Code Online (Sandbox Code Playgroud)

我还尝试在我的 polyfill.ts 文件中导入 'zone.js/dist/zone-patch-rxjs',但它也不起作用。

您可以在https://stackblitz.com/edit/angular-issue-repro2-daytbo 中看到一个具有最少代码的项目来重现它

任何线索或帮助都非常受欢迎!

提前致谢!

Ton*_*Ngo 0

不确定,但你可以尝试这个。

将这些添加到您的polyfill中

import 'core-js/es6';
import 'core-js/es7/reflect';
import "zone.js/dist/zone";
Run Code Online (Sandbox Code Playgroud)

很少有链接可以尝试

https://github.com/angular/zone.js/issues/465

https://github.com/angular/angular/issues/31724