将Crypto-JS导入Angular 2项目

时间:2017-01-18 12:19:31

标签: angular typescript systemjs cryptojs

我正在使用typescript和SystemJS进行Angular 2开发,并希望包含crypto-js。 这是systemjs.config.js文件:

(function (global) {
  System.config({
    paths: {
      'npm:': 'node_modules/'
    },
    map: {
      app: 'app',

      // 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',
      'crypto-js':                 'npm:crypto-js/core.js',
      'crypto-js/sha256':          'npm:crypto-js/sha256.js',
    },

    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      },
      'crypto-js': {
        defaultExtension: 'js'
      }
    }
  });
})(this);

在service.ts中:

import 'crypto-js/sha256';

当我运行应用程序时,这是我得到的错误:

http://localhost:3000/node_modules/crypto-js/core as "./core" from http://localhost:3000/node_modules/crypto-js/sha256.js

我该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

事实证明,我所要做的就是添加defaultJSExtensions:' js',就像这样:

packages: {
        '.': {
            defaultJSExtensions: 'js'
        },
        app: {
            main: './main.js'
        }
    }