本文介绍了在angular 2项目中导入crypto-js(使用angular-cli创建)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的angular2项目中导入crypto-js.

I'm trying to import crypto-js in my angular2 project.

我关注了几个SO问题以及 angular-cli指南,但最后仍然出现错误找不到模块'crypto-js'

I followed several SO questions and also angular-cli guide, but at the end I still have the error Cannot find module 'crypto-js'

我尝试过的事情:

npm install crypto-js --save

typings install dt~crypto-js --global --save

然后我修改了文件 angular-cli-build.js

var Angular2App = require('angular-cli/lib/broccoli/angular2-app');

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      'systemjs/dist/system-polyfills.js',
      'systemjs/dist/system.src.js',
      'zone.js/dist/**/*.+(js|js.map)',
      'es6-shim/es6-shim.js',
      'reflect-metadata/**/*.+(ts|js|js.map)',
      'rxjs/**/*.+(js|js.map)',
      '@angular/**/*.+(js|js.map)',
      'crypto-js/**/*.+(js|js.map)'
    ]
  });
};

和文件 src/system-config.ts

const map: any = {
    'crypto-js': 'vendor/crypto-js'
};

/** User packages configuration. */
const packages: any = {
    'crypto-js': {
        format: 'cjs'
    }
};

使用后

import * as CryptoJS from 'crypto-js';

我仍然有我的错误.我错过了什么吗?

I still have my error. Did I miss something ?

谢谢

推荐答案

好的,我明白了.我刚刚下载了 DefinitelyTyped 文件,其类型为/crypto-js/,然后在导入CryptoJS之前添加/// <reference path="../../typings/crypto-js/crypto-js.d.ts" />行.

Ok I got it. I just download the DefinitelyTyped file in typings/crypto-js/ and then I add the line /// <reference path="../../typings/crypto-js/crypto-js.d.ts" /> before importing CryptoJS.

这篇关于在angular 2项目中导入crypto-js(使用angular-cli创建)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:20