How to generate UUID with angular 2?

醉酒当歌 提交于 2019-12-18 10:33:19

问题


I'm using Angular 2 for a signup form: first name, last name, email and password.

After submit, the data is being stored via API call in a database (nodeJs and mongo) and generates a JWT Token which is sent back to the client.

Now I should add/generate an UUID (Universal Unique Identifier). As I never have done this kind of feature before, I need an approach and idea/solution how to achieve this... would the JWT Token be a kind of alternative to UUID? If yes, this would be enough.

Otherwise I would prefer to avoid any big changes on the form or its functionality.

I Have been searching, but didn't find a useful solution. I tried the npm package angular2-uuid, but after installing it as dependency, the ng build -prod throws an error which isn't clear.

import { UUID } from 'angular2-uuid';
....
let uuid = UUID.UUID();

Error:

ERROR in ./~/@angular/flex-layout/@angular/flex-layout.es5.js Module build failed: Error: ENOENT: no such file or directory, open '/Users/username/dev/app/node_modules/@angular/flex- layout/@angular/flex-layout.es5.js' @ ./src/$$_gendir/app/app.module.ngfactory.ts 25:0-44 @ ./src/main.ts @ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts

Any idea or Hint please?


回答1:


It's downloaded as a part of Angular's dependency, use it like:

import { v4 as uuid } from 'uuid';

@Component(..)
export class AppComponent {

  console.log('new uid: ', uuid());
}



回答2:


try this (https://github.com/wulfsolter/angular2-uuid)

As the doc say :

import { UuidService } from 'angular2-uuid';

constructor(private uuid: UuidService) //<-- pass it in contructor as service

const uuid = this.uuid.generate(); //<-- use it

Hope it helps you! ..as you can see it changed a little bit from what it report here (https://www.npmjs.com/package/angular2-uuid)




回答3:


Issue fixed. Solution: I found out that when I did install the package angular2-uuid via command-line, it was automatically stored in package.json directly before/over the package: @angular/flex-layout.

As the error was in a way not clear, I just did remove it to other position in the: package.json and the error is gone. everything works fine.



来源:https://stackoverflow.com/questions/45754907/how-to-generate-uuid-with-angular-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!