How to generate UUID in Angular

XYZ picture XYZ · Oct 16, 2018 · Viewed 67.6k times · Source

How do I generate a UUID in Angular?

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,

Answer

Xinan picture Xinan · Oct 16, 2018

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

The code looks like this:

import * as uuid from 'uuid';

const myId = uuid.v4();

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.