【问题标题】:How do I use pure javascript library, hashids.js on angular?如何在 Angular 上使用纯 JavaScript 库 hashids.js?
【发布时间】:2018-09-10 07:01:59
【问题描述】:

我尝试直接导入库

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的续篇)

【问题讨论】:

  • 您是否尝试将import * as hash from '../../../../node_modules/hashids'; 替换为require('hashids')
  • 这样的,先生?要求('hashids');
  • 如何访问这些方法?感谢您的回复!
  • 在require之后,只需输入declare var hashids:any;,然后作为文档使用。
  • 它会抛出这个错误,ERROR ReferenceError: hashids is not defined。我想它确实有它的怪癖。感谢您再次回复先生!

标签: javascript angular typescript hashids


【解决方案1】:

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

import * as hash from 'hashids';

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

【讨论】:

  • wingnod 先生,我把代码扔给了构造函数,减去了导入,但弹出了这个错误。不能将“new”与类型缺少调用或构造签名的表达式一起使用。
  • 它没有使用 import * as hash from 'hashids';但是使用 import * as hash from '../../../../node_modules/hashids/dist/hashids.min.js';
猜你喜欢
  • 1970-01-01
  • 2020-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
相关资源
最近更新 更多