首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 2非cli添加polyfills.ts

Angular 2非cli添加polyfills.ts
EN

Stack Overflow用户
提问于 2017-05-31 03:42:55
回答 1查看 3.1K关注 0票数 2

我有一个angular 2应用程序,我没有使用angular-cli来生成它。我是Angular的新手,我正在尝试让我的应用程序与IE,旧的Safari等一起工作。

我读了很多关于polyfills.ts的文章,我如何将它添加到我的angular 2应用程序中(我正在使用systemjs加载该应用程序)

下面是我目前正在加载的脚本:

代码语言:javascript
复制
<script src = "node_modules/core-js/client/shim.min.js"></script>
<script src = "node_modules/zone.js/dist/zone.js"></script>
<script src = "node_modules/reflect-metadata/Reflect.js" ></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>

和systemjs.config:

代码语言:javascript
复制
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': '/node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
       app: '/js/angular/eventticketspage',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      // other libraries
      'rxjs': 'npm:rxjs',
      'core-js': 'npm:core-js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
        app: {
            main: './main.js',
            defaultExtension: 'js'
        },
        rxjs: {
            defaultExtension: 'js'
        },
        'core-js': {
            defaultExtension: 'js'
        }
    }
  });
})(this);

编辑:我可以通过添加脚本来解析未定义的'intl‘:

代码语言:javascript
复制
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.en"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/classlist/2014.01.31/classList.min.js"></script>

但是我想用正确的方式来做,使用Polyfills.ts

EN

回答 1

Stack Overflow用户

发布于 2017-05-31 14:33:57

看起来你只需要手动完成。根本就没有魔法。我刚刚从https://github.com/angular/angular/blob/master/aio/src/polyfills.ts复制了polyfills.ts,并将其包含在我的源代码文件夹中。我已经取消了对IE的注释,但离开了区域,并将反射行注释掉了。然后在我的应用程序中

代码语言:javascript
复制
import "../shared/pollyfills" // I have put pollyfills.ts in a shared folder
import "zone.js";
import "reflect-metadata";

它导入核心文件,该文件又导入pollyfills.ts -js/es6包。

虽然我使用的是一个模块捆绑器,所以我不确定system.js是如何工作的--但是原理是一样的,复制pollyfills文件并像导入其他模块一样导入它。哦,当然,在你的package.json中你需要

代码语言:javascript
复制
"core-js": "^2.4.1",

或者类似的。

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

https://stackoverflow.com/questions/44270659

复制
相关文章

相似问题

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