2016-06-21 82 views
0

我想在我的Angular 2項目中使用Forge(https://github.com/digitalbazaar/forge)。在角度2服務中的node-forge導入

我運行了以下命令:npm install node-forge 此命令在我的應用程序(在node-modules目錄中)中創建了node-forge目錄。

我在我的package.json文件中添加了node-forge引用:"node-forge": "0.6.39"(依賴關係部分)。

現在,我要導入我的角2服務(打字稿文件)的節點鍛依賴下面的代碼:

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 } from 'node-forge';和我有以下錯誤在控制檯(ng服務命令):

hash-password.service.ts (2, 23): Cannot find module 'node-forge'. 
hash-password.service.ts (11, 16): Cannot find name 'forge'. 

所以,有人知道我怎麼可以導入此節點鍛造依賴(使用npm包)?我錯過了我的過程中的一個步驟?

感謝您的幫助!

回答

1

您需要的打字稿定義以及故宮包..

我不知道如果此包有一個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(); 
    } 
}