Angular 2将字符串转换为md5哈希 [英] Angular 2 convert string to md5 hash
问题描述
我找到了 ts-md5 软件包,但在示例中它有一个 hashStr
方法,但现在却没有。
I found the ts-md5 package but in the example it has a hashStr
method but now it doesn't.
属性'hashStr'不存在在类型
Md5
。
使用该错误后,我的控制台中记录了该错误。我怎么能这样做?
That error is logged in my console after using that. How can I do that?
我试过在构造函数中注入它
I tried inject it in constructor
constructor(private _md5: Md5) {}
然后
let a: any = this._md5.hashStr("password");
推荐答案
我刚检查了文档和源代码, Md5
类的实例上不存在 hashStr
方法。
I just checked out the documentation and source code, and the hashStr
method doesn't exist on instances of the Md5
class.
这意味着如果您只需要使用 hashStr
方法,则无需在构造函数中初始化类,因为您只需调用该方法直接在 Md5
类:
This means that if you only need to use the hashStr
method, you don't need to initialize the class in your constructor since you can just call the method directly on the Md5
class:
let hash = Md5.hashStr("password");
如果你想从实例(而不是类)生成哈希,那么你会使用 appendStr
方法然后链接 end()
方法:
If you want to generate the hash from an instance (rather than the class), then you would use the appendStr
method and then chain the end()
method:
let hash = _md5.appendStr('password').end();
此外,由于您使用的是Angular 2,请记得添加 Md5如果您在构造函数中初始化它,则
组件的 providers
数组中的类:
Also, since you're using Angular 2, remember to add the Md5
class in your component's providers
array if you are initializing it in your constructor:
import { Md5 } from 'ts-md5/dist/md5';
@Component({
// ...
providers: [Md5]
})
export class ExampleComponent {
constructor(
private _md5: Md5
) {
let hash = Md5.hashStr("password");
// or ...
let hash2 = _md5.appendStr('password').end();
}
}
这篇关于Angular 2将字符串转换为md5哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!