node.js - 在 typescript 中使用带有 Angular 6 的 Node 短 ID 模块

标签 node.js angularjs typescript angular6

我尝试在 Angular 6 typescript 中使用 Node 模块。

首先:npm i shortid

In TypeScript class:

import { shortid } from 'shortid';

let Uid = shortid.generate();

但我得到一个错误“找不到函数 generate()”;

如何正确使用?

最佳答案

代替

import { shortid } from 'shortid';

import { generate } from 'shortid';

并用

执行代码
let Uid = generate();

shortid 没有预装 TypeScript 类型,因此您不会获得有关您可以在 Visual Studio Code 等 IDE 中导入、参数和返回值的确切信息。
您可以通过为 shortid 安装类型来解决此问题 - 转到 Microsoft 的 TypeSearch网站,输入“shortid”,您将被重定向到 npm 包 @types/shortid ,您可以使用 npm i @types/shortid 安装它。 您还可以检查 DefinitelyTyped repository直接发布了很多类型的 javascript 包。

安装他们的 typings 后,IDE 将向您显示信息并为 shortid 提供自动完成功能,类似于 native typescript 模块。

关于node.js - 在 typescript 中使用带有 Angular 6 的 Node 短 ID 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54943329/

相关文章:

Node.js "Server listening on port 3000",但我无法在浏览器中查看它?

javascript - 选择框中的 Angular , boolean 值

AngularJS Jasmine 2.0 异步测试超时

typescript - Electron + typescript + 网页包 : Boilerplate Example

javascript - 预期 typescript 错误 'module, class, interface, enum, import or statement'

javascript - 使用 Node.js 和 XPath 对 HTML 页面进行高性能解析

javascript - 子类遵循父类的结构

javascript - AngularJS使用mvc在spa模型中路由无限循环

javascript - 如何在 Angular.io 中使用/实现 Leaflet Bing Layer?

node.js - Tibco EMS 协议(protocol)