Vue生成唯一id

精简版

安装nanoid库(实际上就是一个函数) ,是简化版的uuid。

npm  i  nanoid

使用方式:

<script>
import {nanoid} from "nanoid";

export default {
  name: "MyHeader",
  methods: {
    add(event) {
      const obj = { id: nanoid(), title: event.target.value, done: "false" };
      console.log(obj);
    },
  },
};

完整版

在很多应用场景中,我们经常需要生成唯一ID来作为数据的标识。nanoid 是一个轻量级、高性能的库,可以帮助我们生成这些独特的ID。在本篇文章中,我们将简要介绍 nanoid,并演示如何在Vue项目中使用它。

什么是 nanoid

nanoid 是一个小巧且快速的JavaScript库,用于生成各种长度的全局唯一标识符 (GUID)。它的长度可自定义,默认长度为 21 个字符。与其他类似库相比,nanoid 在生成随机数的过程中使用了一种更佳安全的方法,使得产生 重复ID 的概率变得微乎其微。

安装 nanoid

首先,我们需要在 Vue 项目中安装 nanoid。打开你的终端,然后输入以下命令:

npm install nanoid

或者,如果你正在使用 yarn 作为包管理器:

yarn add nanoid

在 Vue 项目中使用 nanoid

让我们通过一个简单的例子来展示如何在 Vue 项目中使用 nanoid

假设我们在创建一个待办事项应用,其中每个待办事项都有一个唯一的ID。首先,我们需要在项目中引入 nanoid

// 引入 nanoid
import { nanoid } from 'nanoid';

接下来,在 Vue 实例的 methods 或 computed 属性中,我们可以使用 nanoid 函数生成唯一ID:

methods: {
  // 创建一个待办事项
  createTodo: function(newTodo) {
    const taskId = nanoid(); // 生成唯一ID
    // 将带有唯一ID的新待办事项添加到列表中
    this.todos.push({
      id: taskId,
      task: newTodo,
    });
  }
}

这样,每当我们创建一个新的待办事项时,createTodo 函数都会为其生成一个唯一的ID。

总结

nanoid 是一个轻量级、高性能的库,可以帮助我们在 Vue 项目中轻松生成独特的全局唯一ID。它允许自定义长度,且拥有较低的重复ID概率。通过简单的引入和一行代码,我们就可以方便地在 Vue 项目中使用 nanoid 进行ID的生成。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue生成雪花ID可以使用snowflake算法。Snowflake算法是一种分布式唯一ID生成算法,它可以在多个节点上生成不重复的ID。以下是一个示例代码,用于在Vue生成雪花ID: ```javascript // snowflake.js class Snowflake { constructor(datacenterId, workerId) { this.twepoch = 1288834974657n; this.datacenterId = BigInt(datacenterId); this.workerId = BigInt(workerId); this.sequence = 0n; this.workerIdBits = 5n; this.datacenterIdBits = 5n; this.maxWorkerId = -1n ^ (-1n << this.workerIdBits); this.maxDatacenterId = -1n ^ (-1n << this.datacenterIdBits); this.sequenceBits = 12n; this.workerIdShift = this.sequenceBits; this.datacenterIdShift = this.sequenceBits + this.workerIdBits; this.timestampLeftShift = this.sequenceBits + this.workerIdBits + this.datacenterIdBits; this.sequenceMask = -1n ^ (-1n << this.sequenceBits); this.lastTimestamp = -1n; } nextId() { let timestamp = BigInt(Date.now()); if (timestamp < this.lastTimestamp) { throw new Error('Invalid system clock'); } if (timestamp === this.lastTimestamp) { this.sequence = (this.sequence + 1n) & this.sequenceMask; if (this.sequence === 0n) { timestamp = this.tilNextMillis(this.lastTimestamp); } } else { this.sequence = 0n; } this.lastTimestamp = timestamp; return ((timestamp - this.twepoch) << this.timestampLeftShift) | (this.datacenterId << this.datacenterIdShift) | (this.workerId << this.workerIdShift) | this.sequence; } tilNextMillis(lastTimestamp) { let timestamp = BigInt(Date.now()); while (timestamp <= lastTimestamp) { timestamp = BigInt(Date.now()); } return timestamp; } } export default Snowflake; ``` 然后,在你的Vue组件中使用这个Snowflake类生成雪花ID: ```javascript <template> <div> <button @click="generateId">Generate Snowflake ID</button> <p>{{ snowflakeId }}</p> </div> </template> <script> import Snowflake from './snowflake'; export default { data() { return { snowflakeId: null, }; }, methods: { generateId() { const snowflake = new Snowflake(1, 1); // 传入datacenterId和workerId this.snowflakeId = snowflake.nextId().toString(); }, }, }; </script> ``` 这样,当你点击"Generate Snowflake ID"按钮时,就会生成一个唯一的雪花ID,并且将其显示在页面上。请根据你的需求修改datacenterId和workerId参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值