<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>vue计算属性的搜索功能</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<body>
<div id="app">
<div>
<input v-model:type="utext" name="" id="" />
<ul v-for="item in fifname" :key="item.id">
<li>{{item.name}}-{{item.sex}}</li>
</ul>
</div>
</div>
<script>
new Vue({
el: "#app",
data() {
return {
usename: [
{ id: 1, name: "小花", sex: "女" },
{ id: 2, name: "小红", sex: "女" },
{ id: 3, name: "成龙", sex: "男" },
{ id: 4, name: "陈伟", sex: "女" },
{ id: 5, name: "杰哥", sex: "女" },
],
// 用户输入的数值
utext: "",
};
},
computed: {
// 需要返回给谁
fifname() {
return this.usename.filter((p) => {
// 返回的是不属于-1的值,并且下面要使用到this,所以shiyon
return p.name.indexOf(this.utext) !== -1;
});
},
},
});
</script>
</body>
</html>