利用computed计算属性实现模糊搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
<div id="app">
<input type="text" placeholder="搜索" v-model="sousuo">
<table border="1">
<th>编号</th>
<th>英雄</th>
<th>技能</th>
<tr v-for="(item,index) in sousuo1()" :key="item.id">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.jn}}</td>
</tr>
</table>
</div>

利用computed计算属性

点我展示代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var app = new Vue({
el: '#app',
data: {
sousuo: '',
list: [{
"id": 1,
"name": "艾希",
"jn": "射箭"
}, {
"id": 2,
"name": "狐狸",
"jn": "魅惑"
}, {
"id": 3,
"name": "猴子",
"jn": "棍子"
}, {
"id": 4,
"name": "盖伦",
"jn": "大宝剑"
}, {
"id": 5,
"name": "德邦",
"jn": "尖枪"
}, {
"id": 6,
"name": "皇子",
"jn": "旗子"
},

]
},
computed: { //设置计算属性
Search() {
if (this.sousuo) {
return this.list.filter((value) => { //过滤数组元素 this.list就是上面的那个死数据
return value.name.includes(this.sousuo); // 查看value.name里面包含不包含输入的字体
}); //this.sousuo跟上面的输入框是双重绑定
}
}
},
methods: {
sousuo1() {
if (!this.sousuo) {
return this.list;
}
return this.Search
}
},
})