简体   繁体   English

在打字稿中使用带有角度6的节点短ID模块

[英]Use node short id module with angular 6 in typescript

I tried to use the node module in an Angular 6 typescript. 我试图在Angular 6打字稿中使用node模块。

first: npm i shortid 首先:npm我矮子

In TypeScript class:

import { shortid } from 'shortid';

let Uid = shortid.generate();

but i got an error "can not find function generate()"; 但我收到一个错误“找不到函数generate()”;

how to use it correctly ? 如何正确使用它?

Instead of 代替

import { shortid } from 'shortid';

do

import { generate } from 'shortid';

and execute the code with 然后执行代码

let Uid = generate();

shortid doesn't come with TypeScript typings preinstalled, so you won't get information about what exactly you can import, arguments and return values within IDEs like Visual Studio Code. shortid并未预装TypeScript类型,因此您不会获得有关在Visual Studio Code之类的IDE中可以准确导入,参数和返回值的信息。
You can fix this by installing typings for shortid - go to Microsoft's TypeSearch website, enter "shortid" and you'll be redirected to the npm package @types/shortid , which you can install with npm i @types/shortid . 您可以通过安装shortid来解决此shortid -转到Microsoft的TypeSearch网站,输入“ shortid”,您将被重定向到npm软件包@ types / shortid ,可以与npm i @types/shortid一起安装。 You can also check the DefinitelyTyped repository directly, where a lot of types for javascript packages are published. 您还可以直接检查DefinitelyTyped存储库 ,其中发布了许多类型的javascript程序包。

After installing they typings, the IDE will show you information and offer auto completion for shortid similar to native typescript modules. 安装它们后,IDE会向您显示信息,并为shortid提供自动完成功能,类似于本机打字稿模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM