<!DOCTYPE html>
<html>
<head>
<title>Vue汉字模糊搜索</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="searchKeyword" placeholder="请输入搜索关键词">
<ul>
<li v-for="item in filteredData" :key="item">{{ item }}</li>
</ul>
</div>
<script>
new Vue({
el: '#app',
data: {
searchKeyword: '',
data: [
"苹果",
"香蕉",
"蓝莓",
"樱桃",
"葡萄",
"柠檬",
"橙子",
"草莓"
]
},
computed: {
filteredData() {
const keyword = this.searchKeyword.trim();
if (keyword) {
const regex = new RegExp(keyword, 'i');
return this.data.filter(item => regex.test(item));
} else {
return this.data;
}
}
}
});
</script>
</body>
</html>