shell
npm install uuid
js
import React from 'react';
import { v4 as uuidv4 } from 'uuid';
const MyComponent = () => {
const uuid = uuidv4();
return (
<div>
<h1>Generated UUID: {uuid}</h1>
</div>
);
}
export default MyComponent;
UUID 各种示例
- 生成基于时间戳的UUID(版本4):
javascript
const { v4: uuidv4 } = require('uuid');
const uuid = uuidv4();
console.log(uuid); // 输出类似于 "110ec58a-a0f2-4ac4-8393-c866d813b8d1"
- 生成基于命名空间和名称的UUID(版本5):
javascript
const { v5: uuidv5, v4: uuidv4 } = require('uuid');
const namespace = '1b671a64-40d5-491e-99b0-da01ff1f3341';
const name = 'Hello, World!';
const uuid = uuidv5(name, namespace);
console.log(uuid); // 输出类似于 "630eb68f-ba71-53d2-94d7-8ab8212d4f8c"
- 解析UUID字符串:
javascript
const { parse: uuidParse } = require('uuid');
const uuidString = '110ec58a-a0f2-4ac4-8393-c866d813b8d1';
const uuid = uuidParse(uuidString);
console.log(uuid); // 输出解析后的UUID对象
- 验证UUID字符串:
javascript
const { validate: uuidValidate } = require('uuid');
const uuidString = '110ec58a-a0f2-4ac4-8393-c866d813b8d1';
const isValid = uuidValidate(uuidString);
console.log(isValid); // 输出 true 或 false