赞
踩
你可以通过使用 AJAX 或者 Fetch API 将参数传递给后端来实现搜索功能。以下是一个示例代码:
// 假设你有一个搜索表单,其中包含一个输入框和一个提交按钮 <template> <form @submit.prevent="search"> <input type="text" v-model="keyword" placeholder="输入关键字"> <button type="submit">搜索</button> </form> </template>
<script> export default { data() { return { keyword: '' // 用于存储用户输入的关键字 } }, methods: { search() { // 创建一个 FormData 对象,用于存储参数 const formData = new FormData() formData.append('keyword', this.keyword) // 使用 Fetch API 发送 POST 请求到后端 fetch('/search', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { // 处理后端返回的数据 console.log(data) }) .catch(error => { console.error(error) }) } } } </script>
在上述示例中,我们使用了 Vue 的双向绑定功能将用户输入的关键字绑定到 keyword
变量上。当用户点击搜索按钮时,search
方法会被调用。该方法使用 Fetch API 发送 POST 请求到后端,并将关键字作为参数传递给后端的 /search
接口。你可以根据你的实际后端接口地址来修改这里的路径。在成功获取到后端返回的数据后,你可以根据需要进行处理。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。