如何在angular上使用纯JavaScript库hashids.js?

时间:2018-09-10 07:01:59

标签: javascript angular typescript hashids

我尝试直接导入库

import * as hash from '../../../../node_modules/hashids';

并尝试了此代码

let id = hash.encode(this.studentDocument.student_id_number); console.log(id);

可惜它抛出了这个错误。

_node_modules_hashids__WEBPACK_IMPORTED_MODULE_2__.encode is not a function

我什至尝试过

declare var hash:any;

但是它抛出了这个错误

hash is not defined

任何提示将不胜感激! (此post的续集)

1 个答案:

答案 0 :(得分:2)

您需要创建一个新的hashids对象实例。

import * as hash from 'hashids';

const hashids = new hash();
const id = hashids.encode(348029348);
console.log(id);