How to create GUID in angular-2?

Please refer to this link, or try this npm package.


You even can use npm to generate this for you. Follow this :

npm i guid-typescript --save

And to utilize in your code :

import { Guid } from 'guid-typescript';
export class GuidExample {
    public id: Guid;
    constructor() {
        this.id = Guid.create(); // ==> b77d409a-10cd-4a47-8e94-b0cd0ab50aa1
    }
}

You can use this code to generate your own GUID:

class GuidGenerator {
    static newGuid() {
      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random() * 16 | 0,
        v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
      });
    }
}