Javascript - 如何在Vue.js中搜索大小写?
Javascript - How to search no matter upperCase or lowercase in Vue.js?
我正在使用 Vue.js 3,我在这里搜索了数据,但该数据来自假 API。所以这个搜索只适用于小字符或大字符,但我想搜索无论大写还是小写的抛出数据?
这是我的代码
<template>
<div class='home'>
<h1>Third</h1>
<div v-if="error"> {{error}} </div>
<div v-if="posts.length">
<input type="text" v-model.trim="search" />
<!-- <p>search term - {{search}}</p> -->
<div v-for="post in matchingNames" :key='post.id'>
<h3>{{post.title }}</h3>
<h1>{{post.id}}</h1>
</div>
</div>
<div v-else> Loading...</div>
</div>
</template>
<script>
import { computed, ref, watch, watchEffect} from 'vue'
// import Listing from '../components/Listing.vue'
import getPosts from '../composables/getPosts'
export default {
name:'Third',
components: {} ,
setup(){
const search = ref('')
const{posts,error,load} = getPosts()
load()
const matchingNames = computed (()=> {
return posts.value.filter((post)=>post.title.match(search.value))
})
return {posts, error, search, matchingNames}
}
}
</script>
<style>
</style>
我也试过用include js方法替换match方法,结果还是一样
这是一张 gif,展示了它现在是如何工作的,谁能帮助我我是 Vue.js 3
的新手
先将两者都转换为小写:)
...
return posts.value.filter((post)=>post.title.toLowerCase().includes(search.value.toLowerCase()))
...
我正在使用 Vue.js 3,我在这里搜索了数据,但该数据来自假 API。所以这个搜索只适用于小字符或大字符,但我想搜索无论大写还是小写的抛出数据? 这是我的代码
<template>
<div class='home'>
<h1>Third</h1>
<div v-if="error"> {{error}} </div>
<div v-if="posts.length">
<input type="text" v-model.trim="search" />
<!-- <p>search term - {{search}}</p> -->
<div v-for="post in matchingNames" :key='post.id'>
<h3>{{post.title }}</h3>
<h1>{{post.id}}</h1>
</div>
</div>
<div v-else> Loading...</div>
</div>
</template>
<script>
import { computed, ref, watch, watchEffect} from 'vue'
// import Listing from '../components/Listing.vue'
import getPosts from '../composables/getPosts'
export default {
name:'Third',
components: {} ,
setup(){
const search = ref('')
const{posts,error,load} = getPosts()
load()
const matchingNames = computed (()=> {
return posts.value.filter((post)=>post.title.match(search.value))
})
return {posts, error, search, matchingNames}
}
}
</script>
<style>
</style>
我也试过用include js方法替换match方法,结果还是一样
这是一张 gif,展示了它现在是如何工作的,谁能帮助我我是 Vue.js 3
的新手先将两者都转换为小写:)
...
return posts.value.filter((post)=>post.title.toLowerCase().includes(search.value.toLowerCase()))
...