Ionic2 离子2导入库

Ionic2 离子2导入库,ionic2,Ionic2,我正在尝试用离子2实现 我已经做了以下工作: 我的index.d.ts现在包含: // 已在/typings/global下创建了crypto js文件夹 然后,我尝试以下代码: declare var require: any; import * as CryptoJS from 'crypto-js'; ... private CryptoJS: any; constructor() { this.CryptoJS = require("crypto-js");

我正在尝试用离子2实现

我已经做了以下工作:

我的
index.d.ts
现在包含:

//

已在
/typings/global
下创建了
crypto js
文件夹

然后,我尝试以下代码:

declare var require: any;
import * as CryptoJS from 'crypto-js';

...

private CryptoJS: any;

    constructor() {
        this.CryptoJS = require("crypto-js");
    }

    test() {
       alert(this.CryptoJS);
    }
只要我尝试引用
this.CryptoJS
(即
alert(this.CryptoJS)
),应用程序就会崩溃

我在导入
crypto js
库的过程中出错了。有人能给我建议吗

谢谢

更新:

import { Injectable } from "@angular/core";
import { LanguageModel } from './languageModel';
import { LocationModel } from './locationModel';
import { JobModel } from './jobModel';
import 'crypto-js';

@Injectable()
export class PersonModel {

    public id: number = null;
    public joiningDate: number = null;
    public lastAccessDate: number = null;
    public userName: string = null;
    public password: string = null;
    public firstName: string = null;
    public lastName: string = null;
    public emailAddress: string = null;
    public locations: LocationModel[] = [];
    public languages: LanguageModel[] = [];
    public time: string = null;
    public avatar: string = null;
    public avatar64: string = null;

    //private CryptoJS: any;
    private SECERET_KEY: string = 'secret key 123';

    public getPasswordEcrypted(): string {
        // Decrypt 
        var bytes = CryptoJS.AES.decrypt(this.password.toString(), this.SECERET_KEY);
        var plaintext = bytes.toString(CryptoJS.enc.Utf8);
        console.log('getPasswordEcrypted', plaintext);
        return plaintext;
    }

    public setPasswordEncrypted(password: string): void {
        // Encrypt 
alert(password);
        console.log('setPasswordEncrypted', password, CryptoJS);
alert(CryptoJS);
        var ciphertext = CryptoJS.AES.encrypt(password, this.SECERET_KEY);
alert(ciphertext);
        console.log('setPasswordEncrypted', password, ciphertext);
        this.password = ciphertext;
    }
}
下面,我运行:

如何在代码中导入
CryptoJS

谢谢

更新:

import { Injectable } from "@angular/core";
import { LanguageModel } from './languageModel';
import { LocationModel } from './locationModel';
import { JobModel } from './jobModel';
import 'crypto-js';

@Injectable()
export class PersonModel {

    public id: number = null;
    public joiningDate: number = null;
    public lastAccessDate: number = null;
    public userName: string = null;
    public password: string = null;
    public firstName: string = null;
    public lastName: string = null;
    public emailAddress: string = null;
    public locations: LocationModel[] = [];
    public languages: LanguageModel[] = [];
    public time: string = null;
    public avatar: string = null;
    public avatar64: string = null;

    //private CryptoJS: any;
    private SECERET_KEY: string = 'secret key 123';

    public getPasswordEcrypted(): string {
        // Decrypt 
        var bytes = CryptoJS.AES.decrypt(this.password.toString(), this.SECERET_KEY);
        var plaintext = bytes.toString(CryptoJS.enc.Utf8);
        console.log('getPasswordEcrypted', plaintext);
        return plaintext;
    }

    public setPasswordEncrypted(password: string): void {
        // Encrypt 
alert(password);
        console.log('setPasswordEncrypted', password, CryptoJS);
alert(CryptoJS);
        var ciphertext = CryptoJS.AES.encrypt(password, this.SECERET_KEY);
alert(ciphertext);
        console.log('setPasswordEncrypted', password, ciphertext);
        this.password = ciphertext;
    }
}

更新:

import { Injectable } from "@angular/core";
import { LanguageModel } from './languageModel';
import { LocationModel } from './locationModel';
import { JobModel } from './jobModel';
import 'crypto-js';

@Injectable()
export class PersonModel {

    public id: number = null;
    public joiningDate: number = null;
    public lastAccessDate: number = null;
    public userName: string = null;
    public password: string = null;
    public firstName: string = null;
    public lastName: string = null;
    public emailAddress: string = null;
    public locations: LocationModel[] = [];
    public languages: LanguageModel[] = [];
    public time: string = null;
    public avatar: string = null;
    public avatar64: string = null;

    //private CryptoJS: any;
    private SECERET_KEY: string = 'secret key 123';

    public getPasswordEcrypted(): string {
        // Decrypt 
        var bytes = CryptoJS.AES.decrypt(this.password.toString(), this.SECERET_KEY);
        var plaintext = bytes.toString(CryptoJS.enc.Utf8);
        console.log('getPasswordEcrypted', plaintext);
        return plaintext;
    }

    public setPasswordEncrypted(password: string): void {
        // Encrypt 
alert(password);
        console.log('setPasswordEncrypted', password, CryptoJS);
alert(CryptoJS);
        var ciphertext = CryptoJS.AES.encrypt(password, this.SECERET_KEY);
alert(ciphertext);
        console.log('setPasswordEncrypted', password, ciphertext);
        this.password = ciphertext;
    }
}
改为:

import CryptoJS from 'crypto-js';
结果:

更新:

import { Injectable } from "@angular/core";
import { LanguageModel } from './languageModel';
import { LocationModel } from './locationModel';
import { JobModel } from './jobModel';
import 'crypto-js';

@Injectable()
export class PersonModel {

    public id: number = null;
    public joiningDate: number = null;
    public lastAccessDate: number = null;
    public userName: string = null;
    public password: string = null;
    public firstName: string = null;
    public lastName: string = null;
    public emailAddress: string = null;
    public locations: LocationModel[] = [];
    public languages: LanguageModel[] = [];
    public time: string = null;
    public avatar: string = null;
    public avatar64: string = null;

    //private CryptoJS: any;
    private SECERET_KEY: string = 'secret key 123';

    public getPasswordEcrypted(): string {
        // Decrypt 
        var bytes = CryptoJS.AES.decrypt(this.password.toString(), this.SECERET_KEY);
        var plaintext = bytes.toString(CryptoJS.enc.Utf8);
        console.log('getPasswordEcrypted', plaintext);
        return plaintext;
    }

    public setPasswordEncrypted(password: string): void {
        // Encrypt 
alert(password);
        console.log('setPasswordEncrypted', password, CryptoJS);
alert(CryptoJS);
        var ciphertext = CryptoJS.AES.encrypt(password, this.SECERET_KEY);
alert(ciphertext);
        console.log('setPasswordEncrypted', password, ciphertext);
        this.password = ciphertext;
    }
}
运行此:

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

结果没有生成错误,但在运行时,当访问
CryptoJS
时(例如
console.log(CryptoJS);
),应用程序崩溃。

您切换到类型是正确的,因为爱奥尼亚使用@types而不是键入

首先

import * as CryptoJS from 'crypto-js';
应该是

   import 'crypto-js';
在这种特定情况下,crypto js不是合适的节点模块,因此导入到常规文件

在typescript中也没有要求,除非您安装了它,而是导入了它。所以这没有意义:

this.CryptoJS = require("crypto-js"); 
这将声明一个空变量:

private CryptoJS: any;
如果您将其转换为javascript,它将如下所示:

var CryptoJS;
import CryptoJS from 'crypto-js';
所以这基本上是非常多余的。您已经拥有导入中的变量

总之,这应该是您的代码:

import CryptoJS from 'crypto-js';


class SomeClass{

       test(){
           alert(CryptoJS);
        }
}
这就是解决办法

安装npm库之后

npm install crypto-js @types/cryptojs --save
在src下创建declaration.d.ts文件,并将declaration添加到模块中。这解决了模块未定义的问题

declare module 'crypto-js';
在服务类本身中,从模块中添加导入CrytoJS,如下所示:

var CryptoJS;
import CryptoJS from 'crypto-js';
现在,您可以在代码中引用CryptoJS。例如:

 let obj = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(['abcd', this.config.salt].join(':'), this.config.salt));

console.log(obj);
产出:

t2eCyUuZMKRNTRqCW0L5KRvMjWhLV887h1ehjCNSr2c=

我不知道这门课是否已经结束,我刚刚为我解决了同样的问题,所以

实际上,您似乎只需要安装crytpoJS,其中包括:

npm install crypto-js --save
并导入要与之一起使用的精确文件/库:

import { SHA3 } from "crypto-js"; // SHA3 one-way encryption
import { AES } from 'crypto-js'; // For AES encryption/decryption
import { enc } from 'crypto-js'; // For characters encodages types (Utf-8, latin1...)
现在您可以直接使用导入库的名称来使用CryptoJs函数:

//AES
let encrypted = AES.encrypt('my message', 'secret key 123');
let bytes  = AES.decrypt(encrypted.toString(), 'secret key 123');
let decrypted = bytes.toString(enc.Utf8);

//SHA
let hash = SHA3('password', { outputLength: 224 }).toString()
也许有一天它会帮助某人


问候。

这应该是前进的方向:你能告诉我你从哪里得到的秘密钥匙吗?私钥:字符串='secretkey 123';它是用户定义的吗?谢谢你的帮助。很好的解释。但是,在构建应用程序时,
导入
行中出现以下错误:
在./app/pages/model/personModel.ts(5,22)中出错:错误TS2307:找不到模块“crypto js”。
(我以前做过此操作:
npm安装--save@types/cryptojs
)如果我运行以下命令:
typings install dt~crypto-js--global--save
,我会得到以下生成错误:
错误在./app/pages/model/personModel.ts(5,8):错误TS1192:模块“crypto-js”没有默认导出。
它应该是导入“crypto-js”;不从“crypto js”导入CryptoJs;我编辑了我的回答谢谢,如果我把它改成
import'crypto js'
,它构建时没有错误,但我遇到了与以前相同的问题,当我运行应用程序时,当我访问
CryptoJS
,应用程序就会崩溃。我已经完成了以下操作:
npm install CryptoJS
npm install--save@types/CryptoJS