简体   繁体   中英

Use node short id module with angular 6 in typescript

I tried to use the node module in an Angular 6 typescript.

first: npm i shortid

In TypeScript class:

import { shortid } from 'shortid';

let Uid = shortid.generate();

but i got an error "can not find function 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.
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 . You can also check the DefinitelyTyped repository directly, where a lot of types for javascript packages are published.

After installing they typings, the IDE will show you information and offer auto completion for shortid similar to native typescript modules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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