惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
N
News and Events Feed by Topic
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
The Cloudflare Blog
H
Heimdal Security Blog
Schneier on Security
Schneier on Security
Engineering at Meta
Engineering at Meta
Google Online Security Blog
Google Online Security Blog
宝玉的分享
宝玉的分享
AI
AI
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
T
Troy Hunt's Blog
S
Security @ Cisco Blogs
H
Hacker News: Front Page
F
Fortinet All Blogs
博客园_首页
S
Secure Thoughts
N
News and Events Feed by Topic
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
Spread Privacy
Spread Privacy
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Hugging Face - Blog
Hugging Face - Blog
Hacker News: Ask HN
Hacker News: Ask HN
C
CXSECURITY Database RSS Feed - CXSecurity.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
L
LINUX DO - 最新话题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
Know Your Adversary
Know Your Adversary
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Scott Helme
Scott Helme
P
Privacy & Cybersecurity Law Blog
S
Securelist
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
O
OpenAI News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
PCI Perspectives
PCI Perspectives
L
LangChain Blog
雷峰网
雷峰网
Security Archives - TechRepublic
Security Archives - TechRepublic
V2EX - 技术
V2EX - 技术

博客园 - 子墨老师

基于Hutool的poi导出excel表格【升级更新】 如何设置wps单元格下拉选项设置 Caused by: org.jasypt.exceptions.EncryptionOperationNotPossibleException: Encryption raised an exception ZoomIt的使用与快捷键 springboot项目中使用Java 8的日期时间API 基于springboot系统,如何跟踪会话过期,浏览器会话标识是否收到正常响应,存储,并在后续请求保持携带 SpringBoot+MyBatis实现数据库字段加密 Excel导出问题:accessExternalStylesheet 解构赋值+扩展运算符在数组和对象上的应用例子 收藏一下JDK下载地址 介绍几个axios接口请求顺序的问题 vue cli的介绍 Failed to start nginx.service: Unit nginx.service not found. 如何实现文件批量重命名后再进行批量打包下载 如何能成功在centos7下安装nodejs18+以上版本 centos7下卸载nodejs源码包 基于ConcurrentMap锁机制的NFS文件合并方案 基于ConcurrentMap锁机制的NFS分片上传方案 如何将已经存在的本地项目源码关联到远程git仓库中 gitee如何使用 centos7安装php+wordpress
Vue2中能否实现输入中文自动转化为拼音, 且不带音调
子墨老师 · 2025-12-19 · via 博客园 - 子墨老师

vue2中能否实现输入中文自动转化为拼音, 且不带音调。有以下几种方案

方案一:使用pinyin库(推荐)

1.安装依赖

npm install pinyin

2.在Vue组件中使用

<template>
  <div>
    <input 
      v-model="chineseInput" 
      placeholder="输入中文"
      @input="convertToPinyin"
    />
    <div>
      <p>中文: {{ chineseInput }}</p>
      <p>拼音: {{ pinyinOutput }}</p>
    </div>
  </div>
</template>

<script>
import pinyin from 'pinyin'

export default {
  data() {
    return {
      chineseInput: '',
      pinyinOutput: ''
    }
  },
  methods: {
    convertToPinyin() {
      // 使用pinyin库转换,设置style为NORMAL去除音调
      const result = pinyin(this.chineseInput, {
        style: pinyin.STYLE_NORMAL, // 不带音调
        heteronym: false // 不启用多音字模式
      })
      
      // 将二维数组转换为一维字符串
      this.pinyinOutput = result.flat().join('')
    }
  }
}
</script>

方案二:自定义指令实现

1.创建自定义指令

// directives/pinyin.js
import pinyin from 'pinyin'

export const pinyinDirective = {
  bind(el, binding, vnode) {
    const vm = vnode.context
    const expression = binding.expression
    
    el.addEventListener('input', (event) => {
      const result = pinyin(event.target.value, {
        style: pinyin.STYLE_NORMAL
      })
      
      const pinyinText = result.flat().join('')
      
      // 更新绑定的数据
      vm[expression] = pinyinText
    })
  }
}

// 在main.js中注册全局指令
import Vue from 'vue'
import { pinyinDirective } from './directives/pinyin'

Vue.directive('pinyin', pinyinDirective)

2.在组件中使用指令

<template>
  <div>
    <input v-model="chineseText" placeholder="输入中文" />
    <input v-pinyin="pinyinText" placeholder="这里显示拼音" />
    <p>拼音结果: {{ pinyinText }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chineseText: '',
      pinyinText: ''
    }
  }
}
</script>

方案三:使用计算属性

<template>
  <div>
    <input v-model="chineseInput" placeholder="输入中文" />
    <p>拼音: {{ pinyinResult }}</p>
  </div>
</template>

<script>
import pinyin from 'pinyin'

export default {
  data() {
    return {
      chineseInput: ''
    }
  },
  computed: {
    pinyinResult() {
      if (!this.chineseInput) return ''
      
      const result = pinyin(this.chineseInput, {
        style: pinyin.STYLE_NORMAL
      })
      
      return result.flat().join('')
    }
  }
}
</script>

方案四:带防抖的优化版本

<template>
  <div>
    <input 
      v-model="chineseInput" 
      placeholder="输入中文"
      @input="debouncedConvertPinyin"
    />
    <p>拼音: {{ pinyinOutput }}</p>
  </div>
</template>

<script>
import pinyin from 'pinyin'

export default {
  data() {
    return {
      chineseInput: '',
      pinyinOutput: '',
      timeout: null
    }
  },
  methods: {
    convertToPinyin() {
      const result = pinyin(this.chineseInput, {
        style: pinyin.STYLE_NORMAL
      })
      this.pinyinOutput = result.flat().join('')
    },
    
    debouncedConvertPinyin() {
      // 防抖处理,避免频繁转换
      clearTimeout(this.timeout)
      this.timeout = setTimeout(() => {
        this.convertToPinyin()
      }, 300)
    }
  },
  
  beforeDestroy() {
    clearTimeout(this.timeout)
  }
}
</script>

方案五:使用其他拼音库

如果不使用pinyin库,也可以使用考虑其他替代方案

使用tiny-pinyin

npm install tiny-pinyin
<script>
import { pinyin } from 'tiny-pinyin'

export default {
  methods: {
    convertToPinyin(text) {
      return pinyin(text, { toneType: 'none' }) // 不带音调
    }
  }
}
</script>

注意事项

  • 性能考虑:对于大量文本转换,建议使用防抖或节流
  • 多音字处理:上述示例关闭了多音字模式,如需处理多音字需要额外逻辑
  • 非中文字符:拼音库通常会保留非中文字符不变
  • 空格处理:可根据需求决定是否保留空格
  • 推荐使用方案一或方案三,它们实现简单且易于维护。