繁体   English   中英

Typescript Node.js 应用程序中的 guid/uuid

[英]guid/uuid in Typescript Node.js app

我尝试使uuid (v 3.0.1) package 在 Node/Typescript 应用程序中工作,但我不确定应该导入什么以及如何使用它。

这是index.d.ts (来自@types/uuid v 2.0.29):

declare namespace uuid {
    interface V1Options {
        node?: number[];
        clockseq?: number;
        msecs?: number | Date;
        nsecs?: number;
    }

    type V4Options = { random: number[] } | { rng: () => number[]; }

    interface UuidStatic {
        (options?: V4Options): string;
        (options: V4Options | null, buffer: number[], offset?: number): number[];
        (options: V4Options | null, buffer: Buffer, offset?: number): Buffer;

        v1(options?: V1Options): string;
        v1(options: V1Options | null, buffer: number[], offset?: number): number[];
        v1(options: V1Options | null, buffer: Buffer, offset?: number): Buffer;
        v4: UuidStatic;
        parse(id: string): number[];
        parse(id: string, buffer: number[], offset?: number): number[];
        parse(id: string, buffer: Buffer, offset?: number): Buffer;
        unparse(buffer: number[] | Buffer, offset?: number): string;
    }
}

declare const uuid: uuid.UuidStatic
export = uuid

我在这里找不到导出的 class。

例如index.d.ts angular2-uuid的 index.d.ts 看起来像这样:

export declare class UUID {
    constructor();
    static UUID(): string;
    private static pad4(num);
    private static random4();
}

使用起来很明显:

let id = UUID.UUID();

所以。 如何使用(导入和调用) uuid

是的,这是我项目中的代码:

import { v4 as uuid } from 'uuid';
const id: string = uuid();

注意:要安装定义,需要运行

npm install --save-dev @types/uuid

根据测试:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/354cec620daccfa0ad167ba046651fb5fef69e8a/types/uuid/uuid-tests.ts

import uuid = require('uuid');

let uuidv4: string = uuid.v4();
import * as uuid from "uuid";
const id: string = uuid.v4();

这也适用于我:

import uuidv4 from 'uuid/v4'

this.projectId = uuidv4()

我按以下顺序为我的项目使用 uuid

必须安装

npm i --save-dev @types/uuid
npm install uuid

使用 uuid package

import { v4 as uuidv4 } from 'uuid';
const myuuid = uuidv4();
console.log('Your UUID is: 'myuuid);

暂无
暂无

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

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