Angular 7: npm 包中 'Class' 需要 polyfill

Angular 7: need polyfill for 'Class' in npm package

我遇到的问题是我基于 Angular 7 的应用程序无法在 IE 11 中运行。

我正在使用从

index.js 开始的 npm 包
class PackageClass { 
   // code
}

并且在所有浏览器中它都工作正常,它做它应该做的。但在 IE11 浏览器中甚至无法打开应用程序,它会失败并显示错误:Syntax error. File: vendor.js line ...。错误导致 class 定义。

我已经在应用程序中的 Polyfills:

/** IE9, IE10 and IE11 requires all of the following polyfills. **/
import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/weak-map';
import 'core-js/es6/set';


/**
 * Required to support Web Animations `@angular/platform-browser/animations`.
 * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation
 **/
// import 'web-animations-js';  // Run `npm install --save web-animations-js`.



/***************************************************************************************************
 * Zone JS is required by default for Angular itself.
 */
import 'zone.js/dist/zone';  // Included with Angular CLI.



/***************************************************************************************************
 * APPLICATION IMPORTS
 */
import 'core-js/es7/object';
import 'core-js/es7/array';

我该如何解决(运行 我的应用程序在 IE11 中)?我认为 polyfill 应该完成这项工作。

JavaScriptclass不支持IE浏览器,可以查看JavaScript Classes Browser compatibility.

在angular 7 应用程序中,如果我们想定义一个class,我们可以使用打字稿创建一个class。

例如,在src/app文件夹中创建一个英雄class,命名为hero.ts。

代码在src/app/hero.ts

export class Hero {
  id: number;
  name: string;
}

然后在组件class中,我们可以导入英雄class,并使用以下代码使用英雄class:

import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {
  hero: Hero = {
    id: 1,
    name: 'Windstorm'
  };

  constructor() { }

  ngOnInit() {
  }

}

更多关于angular申请的详细信息,请查看angular document

此外,还有另一种方法,您可以使用 babel 将您的代码转译为 ES5