首页 > 其他分享 >vue根据汉字添加拼音

vue根据汉字添加拼音

时间:2022-12-28 17:33:06浏览次数:38  
标签:vue 拼音 pinyin pro char 添加 pinyins changePinyin

效果如下 

  • 安装工具库
    npm install pinyin-pro
    或者
    yarn add pinyin-pro
  • 封装组件 change-pinyin
    <template>
        <div class="pinyin">
            <div class="wordBox" v-for="(item,index) in list" :key="index">
                <ruby>{{item['hanzi']}}
                    <rt>{{item['pinyin']}}</rt>
                </ruby>
            </div>
        </div>
    </template>
    <script>
        import { pinyin } from 'pinyin-pro';
        export default {
            name: "change-pinyin",
            props: {
                content: String,
            },
            mounted() {
                for (let char of this.content) {
                  console.log("char==",char);
                    let pinyins = ''
                    // 判断是否为汉字
                    var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
                    if (reg.test(char)) {
                      pinyins = pinyin(char)
                    }
                    this.list.push(
                        {
                            "hanzi": char,
                            "pinyin": pinyins
                        }
                    )
                }
            },
            data() {
                return {
                    list: []
                }
            }
        }
    </script>
    <style scoped>
        .pinyin {
            margin: 5px;
        }
     
        .wordBox {
            width: 2.2em;
            display: inline-block;
            text-align: center;
        }
    </style>
  •  引用组件
    <template>
      <div id="app">
        <change-pinyin content="我们都有一个家,名字叫中国。"></change-pinyin>
      </div>
    </template>
    
    <script>
    import changePinyin from './components/changePinyin.vue';
    
    export default {
      name: "App",
      components: {
        changePinyin,
      },
      mounted() {
      },
    };
    </script>
    
    <style>
    </style>
  • pinyin-pro组件的其他用法,可参考链接:

  https://github.com/zh-lx/pinyin-pro

标签:vue,拼音,pinyin,pro,char,添加,pinyins,changePinyin
From: https://www.cnblogs.com/Hubelian/p/17010851.html

相关文章

  • grafana-连接prometheus添加数据可视化
    1.grafana-连接prometheus添加数据可视化grafana添加prometheus数据可视化Grafana是一个开源的度量分析和可视化系统。部署文档:https://grafana.com/grafana/dow......
  • vue+nuxtJs+vue-monaco制作Monaco Editor编辑器(插件有bug不推荐使用)
    目录前言一、版本二、使用前配置1.插件注册文件2.nuxt.config.js三、使用四、插件bug五、附录1.kind提示图标类型2.默认action前言建议别用,有bug;后续写个不用vue-monaco......
  • prometheus-添加监控linux服务器
    1.prometheus-添加监控linux服务器prometheus添加监控linux服务器node_exporter:用于监控Linux系统的指标采集器。常用指标:CPU内存硬盘网络流量文件描述符系......
  • vue3封装axios并使用拦截器处理错误
    utils/http.jsimportaxiosfrom"axios";consthttp=axios.create({withCredentials:false,timeout:30000,baseURL:"http://127.0.0.1:8000",header......
  • prometheus-添加被监控配置文件详解
    1.prometheus-添加被监控配置文件详解目标(targets):被监控端实例(Instances):每个被监控端称为实例作业(Job):具有相同目标的实例集合称为作业添加配置文件示例scrap......
  • vue项目增加进度条nprogress
    1.安装nprogresscnpminprogress2.在untils文件夹下创建nprogress.ts文件1importNProgressfrom'nprogress';2import'nprogress/nprogress.css';34/......
  • Vue3 富文本编辑器
    官网https://www.wangeditor.com/v5/for-frame.html#%E4%BD%BF%E7%94%A8-1 安装:npminstall@wangeditor/editor--savenpminstall@wangeditor/editor-for-vue@n......
  • vue动态菜单创建icon
    如图,左侧的菜单是动态生成的,前面的icon图标也要动态创建 实现方法:使用vue的 createVNode定义一个生成icon的文件:  createIcon.jsimport*asiconsfrom"@......
  • vue 使用 antv/g2
    效果:安装: npminstall@antv/g2--save引入:import{Chart}from'@antv/g2';使用:<divid="c1"></div>LeftChart(){constchart=newCha......
  • vue3使用sweetalert2替代默认的alert/confirm框
    安装#运行时依赖package.json的dependenciesnpminstallsweetalert2--save#开发时依赖package.json的devDependenciesnpminstallsweetalert2--save-dev ......