关键词搜索

全站搜索
×
密码登录在这里
×
注册会员

已有账号? 请点击

忘记密码

已有账号? 请点击

使用其他方式登录

Vue中使用js-pinyin插件实现中文汉字转拼音

发布2024-01-02 浏览500次

详情内容

在使用vue时,需要有时用到输入中文汉字自动转成拼音,需要用到js-pinyin插件,下面教大家如何安装并使用js-pinyin插件实现中文汉字转拼音。

安装:

npm install js-pinyin --save

属性及方法 :

// setOptions中传入对象,对象可传两个参数
// charCase参数: 输出拼音的大小写模式,0-首字母大写;1-全小写;2-全大写
// checkPolyphone:是否检查多音字 
Pinyin.setOptions({checkPolyphone: false, charCase: 0});

在代码中使用

import Pinyin from 'js-pinyin'

Pinyin.setOptions({ checkPolyphone: false, charCase: 1 })
var pinyin_make = Pinyin.getFullChars(colorForm.name)
 // getCamelChars: 获取拼音首字母
 // getFullChars: 获取拼音
 console.log(Pinyin.getFullChars('徐'));Xu
 console.log(Pinyin.getCamelChars('徐'));X
 
 
 
 <script>
  import Pinyin from 'js-pinyin'
    
  export default {
    methods: {
      test(){
        console.log( Pinyin.getFullChars('明天也要努力') );         // MingTianYeYaoNuLi
        console.log( Pinyin.getCamelChars('明天也要努力') );       //  MTYYNL
        console.log( Pinyin.getCamelChars('12明天也要努力34') );  //  12MTYYNL34
        console.log( Pinyin.getCamelChars('a明天也要努力b') );   //   aMTYYNLb
      }
    },
    mounted(){
      this.test();
    }
  }
</script>


点击QQ咨询
开通会员
上传资源赚钱
返回顶部
×
  • 微信支付
  • 支付宝付款
扫码支付
微信扫码支付
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载