網站搜尋

typescript - 在 Angular 2 專案中導入 Crypto-JS


我正在使用 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

結果,原來我只需要像這樣添加 defaultJSExtensions: 'js':

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