首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Angular 7: npm包中的“Class”需要polyfill

Angular 7: npm包中的“Class”需要polyfill
EN

Stack Overflow用户
提问于 2019-07-10 22:15:53
回答 1查看 1.8K关注 0票数 1

我遇到了一个问题,我的基于Angular 7的应用程序不能在IE 11中工作。

我使用的是npm包,它以index.js开头,从

代码语言:javascript
运行
复制
class PackageClass { 
   // code
}

在所有浏览器中,它都运行得很好,它做它应该做的事情。但在IE11浏览器中,甚至无法打开应用程序,它会失败,并显示一个错误:Syntax error. File: vendor.js line ...。错误导致类定义。

我已经在应用程序中使用的多边形填充:

代码语言:javascript
运行
复制
/** 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中运行我的应用程序)?我认为polyfills应该做这项工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-11 13:31:35

IE浏览器不支持JavaScript类,您可以检查JavaScript Classes Browser compatibility

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

例如,在src/app文件夹中创建一个名为hero.ts的Hero类。

src/app/hero.ts中的代码

代码语言:javascript
运行
复制
export class Hero {
  id: number;
  name: string;
}

然后在component类中,我们可以导入Hero类,并使用以下代码使用Hero类:

代码语言:javascript
运行
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56972692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档