1

I'm building an Ionic 2 (RC0) application and I'm trying to use node-uuid by following the official documentation.

I've done:

$ npm install --save node-uuid
$ npm install --save @types/node-uuid

node-uuid seems to be using the default export approach, so I'm importing it in my typescript file like this:

import uuid from 'node-uuid';

And using it as follows:

console.log(uuid.v4);

However, my app doesn't come up and I see this error in the logs:

TypeError: des$3 is undefined

What am I missing?


Most resources for Angular 2 recommend using the typings CLI to install the type definitions, but this made no difference for me. I tried:

$ npm install --global typings
$ typings install --save node-uuid

$ ionic info

Your system information:

Cordova CLI: You have been opted out of telemetry. To change this, run: cordova telemetry on.
6.3.1

Ionic Framework Version: 2.0.0-rc.0
Ionic CLI Version: 2.1.0
Ionic App Lib Version: 2.1.0-beta.1
OS: Distributor ID: Ubuntu Description: Ubuntu 16.04.1 LTS 
Node Version: v6.6.0

******************************************************
 Dependency warning - for the CLI to run correctly,      
 it is highly recommended to install/upgrade the following:     
 Please install your Cordova CLI to version  >=4.2.0 `npm install -g cordova`
******************************************************
4

2 回答 2

2

请注意 node-uuid 已弃用。他们与另一个项目合并,现在它只被称为uuid。安装 @types 库之前的一切都是正确的。(请注意,您必须仅使用 'uuid' 而不是 'nod-uuid' 重做这些步骤)

然而,

    console.log(uuid.v4);

不生成 id。根据文档,您需要在导入中指定要使用的 uuid 版本,然后将变量作为方法调用:uuid();

来自文档:[弃用警告:不推荐使用 require('uuid'),并且在此模块的 3.x 版之后将不再支持。相反,请使用 require('uuid/[v1|v3|v4|v5]') ,如下例所示。]

下面是一个使用 uuid/v1 的代码示例:

    import { Component } from '@angular/core';
    import uuid  from 'uuid/v1'; //here change 'v1' with the version you desire to use

    @Component({
     selector: "page-uuid",
     templateUrl: "uuid.html"
    })

    export class uuidTestPage {
     id = uuid();

     constructor() {
       console.log(this.id); // outputs id. For example: 298da400-1267-11e8-a6e5-3148ee6706e9
     }
    }

在您提供应用程序并输入 uuidTestPage 后,您应该会看到登录到控制台的 id。id 的格式会因您使用的版本而异:

版本 1(时间戳):我的例子。

版本 3(命名空间)

第 4 版(随机)等...

快乐编码!

于 2018-02-15T16:08:16.750 回答
0

你可以试试:(angular2-uuid

npm install angular2-uuid --save

……

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

它适用于角度 2 和离子 2

于 2017-10-29T13:26:43.690 回答