文章时间:2020年5月28日 13:37:18
解决问题:在搜索框搜索某些东西的时候,我们经常输入了拼音,依然也可以显示搜索出的文字。
所用插件:ChinesePY.js
投稿人:梦群同学
首先在Github下载ChinesePY.js
[下载地址]
import Pinyin from './ChinesePY' // 你的该文件位置
Pinyin.GetJP('中国') // 获取简拼 -> ZH (注意 简拼返回值为大写)
Pinyin.GetQP('中国') // 获取全拼 -> zhongguo
Pinyin.GetHP('中国') // 获取混拼 -> zhongg
Vue.prototype.$pinyin = (restaurant: string, queryString: string): boolean => {
const jp = Pinyin.GetJP(restaurant)
const qp = Pinyin.GetQP(restaurant)
const hp = Pinyin.GetHP(restaurant)
const rgx = new RegExp(queryString, 'gi')
return rgx.test(jp) || rgx.test(qp) || rgx.test(hp)
}
// restaurant -> 需要检索的字符串
// queryString -> 输入的字符串
{
methods: {
handle() {
const value = this.$pinyin('中国', searchString)
if(value) {
// todu something
} else {
// todu something
}
}
}
}