如何在Angular中生成UUID [英] How to generate UUID in Angular

查看:383
本文介绍了如何在Angular中生成UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了软件包 https://www.npmjs.com/package/uuid-generator-ts https://www.npmjs.com/package/@ types/uuid .但是如果我安装了这些软件包,我会得到一个错误,

I tried the packages https://www.npmjs.com/package/uuid-generator-ts and https://www.npmjs.com/package/@types/uuid. But I am getting an error, if I install these packages,

推荐答案

与Angular本身无关,您可以从一种流行的npm软件包中获取uuid,例如:

Nothing to do with Angular itself, you can get uuid from one of the popular npm package such as this:

https://www.npmjs.com/package/uuid

代码如下:

import * as uuid from 'uuid';

const myId = uuid.v4();


但是, uuid 包没有定义 uuid 类(类型);它只是提供了一些实用程序,可用于生成和解析UUID为 string 以及在 string 和字节数组表示形式之间进行转换.因此,您将无法使用类型系统来确保值是有效的UUID.


However, the uuid package does not define a uuid class (type); it just provides utilities for generating and parsing UUIDs as strings, and for converting between string and byte array representations. So you will not be able to use the type system to ensure that values are valid UUIDs.

这篇关于如何在Angular中生成UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆