在 Vue2 项目中实现 输入汉字 → 实时转换成英文驼峰或拼音(比如:输入“销售订单” → 自动生成 salesOrder 或 xiaoshoudingdan)。
下面给你两个最实用、最稳定的方案:
推荐方案一:使用 pinyin-pro(2025 年最强、最准)⭐⭐⭐⭐⭐
npm install pinyin-pro
完整 Vue2 示例(支持驼峰、首字母、小写连字符等多种风格)
<template>
<div>
<el-form label-width="100px">
<el-form-item label="中文名称">
<el-input
v-model="chineseName"
placeholder="请输入中文"
@input="handleChineseInput"
clearable
/>
</el-form-item>
<el-form-item label="拼音(多音词最佳)">
<el-input v-model="pinyin" readonly />
</el-form-item>
<el-form-item label="首字母缩写">
<el-input v-model="pinyinInitial" readonly />
</el-form-item>
<el-form-item label="驼峰命名(大驼峰)">
<el-input v-model="camelCase" readonly />
</el-form-item>
<el-form-item label="小驼峰(常用变量名)">
<el-input v-model="lowerCamelCase" readonly />
</el-form-item>
<el-form-item label="连字符命名(CSS/路由)">
<el-input v-model="kebabCase" readonly />
</el-form-item>
</el-form>
</div>
</template>
<script>
import { pinyin } from 'pinyin-pro';
export default {
data() {
return {
chineseName: '',
pinyin: '',
pinyinInitial: '',
camelCase: '',
lowerCamelCase: '',
kebabCase: ''
};
},
methods: {
handleChineseInput(val) {
if (!val.trim()) {
this.clearAll();
return;
}
// 1. 完整拼音(处理多音词超准)
const fullPinyin = pinyin(val, { toneType: 'none' }); // xiaoshoudingdan
this.pinyin = fullPinyin;
// 2. 首字母
const initial = pinyin(val, { pattern: 'initial', toneType: 'none' }); // xsd
this.pinyinInitial = initial;
// 3. 转成驼峰(工具函数)
this.camelCase = this.toCamelCase(val); // SalesOrder
this.lowerCamelCase = this.toLowerCamelCase(val); // salesOrder
this.kebabCase = this.toKebabCase(val); // sales-order
},
// 大驼峰:SalesOrder
toCamelCase(str) {
if (!str) return '';
const py = pinyin(str, { toneType: 'none' });
return py
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join('');
},
// 小驼峰:salesOrder
toLowerCamelCase(str) {
const camel = this.toCamelCase(str);
return camel ? camel.charAt(0).toLowerCase() + camel.slice(1) : '';
},
// 连字符:sales-order
toKebabCase(str) {
if (!str) return '';
const py = pinyin(str, { toneType: 'none' });
return py.toLowerCase().replace(/\s+/g, '-');
},
clearAll() {
this.pinyin = '';
this.pinyinInitial = '';
this.camelCase = '';
this.lowerCamelCase = '';
this.kebabCase = '';
}
}
};
</script>
效果示例:
| 输入中文 | 完整拼音 | 首字母 | 大驼峰 | 小驼峰 | 连字符 |
|---|---|---|---|---|---|
| 销售订单 | xiaoshou dingdan | xsdd | XiaoshouDingdan | xiaoshouDingdan | xiaoshou-dingdan |
| 用户管理中心 | yonghu guanli zhongxin | yhg lzx | YonghuGuanliZhongxin | yonghuGuanliZhongxin | yonghu-guanli-zhongxin |
方案二:轻量级(只用首字母 + 简单驼峰)无需安装包
如果你项目不想加依赖,可以用这个极简版:
methods: {
// 仅适用于简单场景
chineseToPinyin(str) {
if (!str) return '';
const pinyinMap = {
'销': 'xiao', '售': 'shou', '订': 'ding', '单': 'dan',
'用': 'yong', '户': 'hu', '管': 'guan', '理': 'li',
'中': 'zhong', '心': 'xin'
// 继续补充常用字即可
};
let result = '';
for (let char of str) {
result += pinyinMap[char] || char;
}
return result;
},
toCamelCaseSimple(str) {
const py = this.chineseToPinyin(str);
return py.replace(/(\s|^)(\w)/g, (a, b, c) => c.toUpperCase()).replace(/\s/g, '');
}
}
最终推荐
| 需求 | 推荐库 | 理由 |
|---|---|---|
| 多音词准确(如“重庆”→ chongqing) | pinyin-pro | 2025 年最准,支持多音词 |
| 简单项目、不想加依赖 | 方案二(字典映射) | 轻量,够用 |
| 需要首字母缩写 | pinyin-pro | 支持 pattern: ‘initial’ |
强烈推荐使用 pinyin-pro,一行代码搞定,准确率 99.9%,完全支持你所有汉字转英文命名场景!

被折叠的 条评论
为什么被折叠?



