1.安装依赖(npm有问题就用cnpm)
npm install pinyin-match --save
cnpm install pinyin-match --save
2.引用
import pinyin from 'pinyin-match'
3.关键代码
pinyin.match(data, value); //data匹配的内容,value输入的内容
4.示例
<template><div><el-inputplaceholder="输入关键字进行过滤"v-model="filterText"></el-input><el-treeref="treeRef":props="{ children: 'children', label: 'label'}":data="personData"node-key="userId"show-checkbox:filter-node-method="filterNode":default-checked-keys="dialogObj.smsUsersId"></el-tree></div>
</template><script>import pinyin from 'pinyin-match';export default {data() {return {filterText: '',personData: [],dialogObj:{smsUsersId: []}};},watch: {filterText(val) {this.$refs.treeRef.filter(val);}},methods: {filterNode(value, data) {if (!value) return true;return pinyin.match(data.label, value); //拼音模糊搜索// return data.label.indexOf(value) !== -1; //原版模糊搜索}}};
</script>