vue实现搜索框查询功能
时间: 2023-07-13 09:05:15 浏览: 58
Vue实现搜索框查询功能可以通过以下步骤实现:
1. 在Vue组件中定义一个数据属性,用于存储用户输入的关键字和查询结果。
2. 在模板中添加一个搜索框和一个显示查询结果的区域。
3. 给搜索框绑定一个input事件,当用户输入关键字时更新数据属性中的关键字。
4. 添加一个按钮或者按下回车键时触发查询方法,并将用户输入的关键字作为参数传递给查询方法。
5. 查询方法可以通过Ajax等方式向后端发送请求,获取查询结果,并将结果保存到数据属性中。
6. 在模板中使用v-for指令遍历查询结果,显示到页面上。
示例代码如下:
```html
<template>
<div>
<input type="text" v-model="keyword" @input="search()">
<button @click="search()">搜索</button>
<ul>
<li v-for="(item, index) in results" :key="index">{{ item }}</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
keyword: '',
results: []
}
},
methods: {
search() {
// 发送Ajax请求,获取查询结果
// 这里使用假数据来模拟查询结果
const data = ['搜索结果1', '搜索结果2', '搜索结果3']
this.results = data.filter(item => item.includes(this.keyword))
}
}
}
</script>
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)