汉语转拼音
tui-pinyin — 汉字转拼音工具,支持字符串和数组输出,可配置声调格式。
介绍
tui-pinyin 提供汉字转拼音能力,导入后支持字符串和数组两种输出格式,声调支持默认符号、数字和去掉三种模式。
导入
ts
import { pinyinString, pinyinArray } from '@/uni_modules/tui-pinyin'基础用法
ts
// 字符串输出,带声调符号
const str1 = pinyinString('汉语拼音', null)
// 结果: 'hàn yǔ pīn yīn'
// 字符串输出,数字声调
const str2 = pinyinString('汉语拼音', { toneType: 'num' })
// 结果: 'han4 yu3 pin1 yin1'
// 数组输出,带声调符号
const arr1 = pinyinArray('汉语拼音', null)
// 结果: ['hàn', 'yǔ', 'pīn', 'yīn']
// 数组输出,无声调
const arr2 = pinyinArray('汉语拼音', { toneType: 'none' })
// 结果: ['han', 'yu', 'pin', 'yin']
// 数组输出,数字声调
const arr3 = pinyinArray('汉语拼音', { toneType: 'num' })
// 结果: ['han4', 'yu3', 'pin1', 'yin1']API
pinyinString
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| text | 待转换的汉字字符串 | String | — |
| options | 配置项 | PinyinOptions | null | null |
返回:String
pinyinArray
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| text | 待转换的汉字字符串 | String | — |
| options | 配置项 | PinyinOptions | null | null |
返回:String[]
Options
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| toneType | 声调输出格式 | 'none' | 'num' | null | null |
null:带声调符号(如 hàn)'none':无声调(如 han)'num':数字声调(如 han4)
完整示例
参考 demo:demo/pinying/pinying.uvue
vue
<template>
<view>
<text>{{ pinstr }}</text>
<text>{{ pinstrnum }}</text>
<text>{{ pinarray }}</text>
<text>{{ pinarraynone }}</text>
<text>{{ pinarraynum }}</text>
</view>
</template>
<script setup>
import { pinyinString, pinyinArray } from '@/uni_modules/tui-pinyin'
const pinstr = ref(pinyinString('汉语拼音', null))
const pinstrnum = ref(pinyinString('汉语拼音', { toneType: 'num' }))
const pinarray = ref(JSON.stringify(pinyinArray('汉语拼音', null)))
const pinarraynone = ref(JSON.stringify(pinyinArray('汉语拼音', { toneType: 'none' })))
const pinarraynum = ref(JSON.stringify(pinyinArray('汉语拼音', { toneType: 'num' })))
</script>