Skip to content

实现 随机用户名 工具

分类:general | 标签:用户名、昵称、随机 组合形容词名词生成用户名

2.1 功能说明

组合形容词名词生成用户名

2.1.1 使用指南

功能说明

组合形容词 + 名词 + 数字,生成随机用户名,可批量。

使用场景

注册占位、测试账号、创意昵称。

2.2 代码实现

2.2.1 组件结构

本工具是一个基于 Vue 3 <script setup> 语法的单文件组件(SFC),统一包裹在 ToolPageShell 组件内,由它提供页面标题、工具 ID、分类与「使用指南」插槽等通用外壳;核心业务逻辑(响应式状态、计算属性、事件处理函数)全部写在 <script setup> 中,输入/输出通过 el-inputel-button 等 Element Plus 组件与用户交互,所有数据均在浏览器本地处理,不会上传服务器。

2.2.2 核心逻辑概览

<script setup> 中定义的主要函数/方法:

  • gen()
  • copy()
  • clear()

2.2.3 关键实现代码

以下是该工具的完整 <script setup> 实现(核心代码)。模板(<template>)与样式(<style>)已省略。

vue
import { ref } from 'vue'
import ToolPageShell from '@/components/ToolPageShell.vue'

const ADJ = ['cool', 'silent', 'dark', 'bright', 'swift', 'calm', 'wild', 'lucky', 'tiny', 'brave', 'golden', 'crimson', 'frost', 'storm', 'neon', 'cosmic']
const NOUN = ['tiger', 'river', 'wolf', 'star', 'moon', 'fox', 'cloud', 'echo', 'nova', 'panda', 'falcon', 'maple', 'comet', 'willow', 'raven', 'ember']
const count = ref(10), sep = ref('_'), withNum = ref(true)
const list = ref([])

function gen() {
  const n = Math.min(Math.max(Number(count.value) || 1, 1), 200)
  const out = []
  for (let i = 0; i < n; i++) {
    let u = ADJ[Math.floor(Math.random() * ADJ.length)] + sep.value + NOUN[Math.floor(Math.random() * NOUN.length)]
    if (withNum.value) u += sep.value + Math.floor(Math.random() * 1000)
    out.push(u)
  }
  list.value = out
}
function copy() { if (list.value.length) navigator.clipboard?.writeText(list.value.join('\n')) }
function clear() { list.value = [] }

2.3 效果截图

随机用户名 效果截图

工具访问地址:https://www.i91tools.com/tools/username-generator