Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js angular 2服务中的节点forge导入_Node.js_Angular_Npm - Fatal编程技术网

Node.js angular 2服务中的节点forge导入

Node.js angular 2服务中的节点forge导入,node.js,angular,npm,Node.js,Angular,Npm,我正在尝试在Angular 2项目中使用Forge() 我运行了以下命令:npm安装节点forge 此命令在我的应用程序中创建了node forge目录(在node modules目录中) 我在package.json文件中添加了node forge引用:“node forge”:“0.6.39”(依赖项部分) 现在,我想用以下代码导入angular 2服务(typescript文件)中的node forge依赖项: import { Injectable } from '@angular/co

我正在尝试在Angular 2项目中使用Forge()

我运行了以下命令:
npm安装节点forge
此命令在我的应用程序中创建了node forge目录(在node modules目录中)

我在package.json文件中添加了node forge引用:
“node forge”:“0.6.39”
(依赖项部分)

现在,我想用以下代码导入angular 2服务(typescript文件)中的node forge依赖项:

import { Injectable } from '@angular/core';
import { Forge } from 'node-forge';

@Injectable()
export class HashPasswordService {
  constructor() {}
  buildHash(input: string) {
    var hmac = forge.hmac.create();
    hmac.start('sha512', input);
    hmac.update(input);
    return hmac.digest().toHex();
  }
}
但是导入不起作用:
import{Forge}来自'node Forge'和我在控制台中有以下错误(ng serve命令):

那么,有人知道我如何导入这个NodeForge依赖项(使用npm包)?我是否错过了流程中的一个步骤


谢谢你的帮助

您需要类型脚本定义以及npm包

我不确定这个包是否有一个
DefinitelyTyped
包,所以你可以试试

npm install typings -g
typings install node-forge
如果这不起作用,请尝试:

import { Injectable } from '@angular/core';

declare var Forge: any;

@Injectable()
export class HashPasswordService {
  private forge: any;

  constructor() {
     this.forge = new Forge();
  }

  buildHash(input: string) {
    var hmac = forge.hmac.create();
    hmac.start('sha512', input);
    hmac.update(input);
    return hmac.digest().toHex();
  }
}

只需从“node forge”导入*as forge即可

import { Injectable } from '@angular/core';

declare var Forge: any;

@Injectable()
export class HashPasswordService {
  private forge: any;

  constructor() {
     this.forge = new Forge();
  }

  buildHash(input: string) {
    var hmac = forge.hmac.create();
    hmac.start('sha512', input);
    hmac.update(input);
    return hmac.digest().toHex();
  }
}