<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="s" @input="handleInput">
<ul>
<li v-for="str in strList2" v-text="str"></li>
</ul>
</div>
</body>
<script>
const app = Vue.createApp({
data() {
return {
s: '',
strList: ['a', 'ab', 'abc', 'abcd', 'abcde', 'abcdef', 'abcdefg'],
strList2: [],
}
},
methods: {
handleInput() {
//箭头函数没有自己的this
this.strList2 = this.strList.filter(item => {
//indexOf不存在返回-1,存在返回索引
return item.indexOf(this.s) > -1
})
}
}
})
app.mount("#app")
</script>
</html>
结果