vue生成唯一id

注意:需要声明一个字符串来接受这个生成的唯一id

  createUniqueId(n) {
      n = 1; // 生成一个唯一id,包含数字和字母
      var random = function() {
        // 生成10-12位不等的字符串
        return Number(
          Math.random()
            .toString()
            .substr(2)
        ).toString(36); // 转换成十六进制
      };
      var arr = [];
      function createId() {
        var num = random();
        var _bool = false;
        arr.forEach(v => {
          if (v === num) _bool = true;
        });
        if (_bool) {
          createId();
        } else {
          arr.push(num);
        }
      }
      var i = 0;
      while (i < n) {
        createId();
        i++;
      }
      this.uniqueId = arr[0]; // 将生成的转为我们需要的字符串并赋值
      return arr;
    },
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
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参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值