【发布时间】:2021-11-25 03:42:08
【问题描述】:
我正在尝试创建一个呈现多个引号并允许您搜索数据的 Vue 应用程序。我想也许我没有将函数绑定到正确的元素。我在输入标签上有一个 v-model 属性,所以它应该绑定用户输入的任何文本。
这是我的模板:
<template>
<div class="container">
<h3>Quotes</h3>
<div class="controllers">
<div class="search">
<input id="search-item" type="text" placeholder="Search for a quote..." v-model="searchText"/>
<button @click="searchResults()" class="search-btn">Search</button>
</div>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">Source</th>
<th scope="col">Context</th>
<th scope="col">Quote</th>
<th scope="col">Theme</th>
</tr>
</thead>
<tbody>
<tr v-for="quote in quotes" v-bind:key="quote.quote">
<th scope="row">{{quote.context}}</th>
<td>{{quote.source}}</td>
<td>{{quote.quote}}</td>
<td>{{quote.theme}}</td>
</tr>
</tbody>
</table>
</div>
</template>
这是我的脚本
import axios from 'axios';
export default {
name: 'Quotes',
data() {
return {
searchText: '',
// search: null,
// data: null,
quotes: [
{
quote: null,
context: null,
source: null,
theme: null,
currentPage: 0,
}
],
};
},
mounted() {
axios
.get('https://gist.githubusercontent.com/benchprep/dffc3bffa9704626aa8832a3b4de5b27/raw/quotes.json')
.then(res => {
this.quotes = res.data;
})
},
methods: {
searchResults() {
if (this.searchText.length === 0) {
return '';
}
return this.quotes.filter(quote => quote.quote.match(this.searchText));
},
}
}
当我尝试实现搜索时,该功能不起作用。我在编译器中没有收到任何错误。
【问题讨论】:
-
searchText永远不会改变我所看到的,v-model="filter"也没有被使用,我认为你的错误是v-model="filter"它应该是v-model="searchText" -
啊,是的,我看到了那个错误。我刚改了
v-model="searchText"还是无法搜索表格。
标签: javascript vue.js vuejs2