debounce(func, wait = 500, immediate = false)
在连续的操作中,无论进行了多长时间,只有某一次的操作后在指定的时间内没有再操作,这一次才被判定有效
需要搜索功能的页面,click事件一般用于跳转到搜索页
<template>
<view>
<view>
<u-search margin="10rpx" v-model="keyword" disabled @click="go_search"></u-search>
</view>
</view>
</template>
<script>
export default {
data() {
return {
keyword: '搜点什么...'//这里做好是后端给的数据
}
},
methods: {
go_search(){
let that = this
uni.navigateTo({
url: '/pages/search/search?keyword=' + that.keyword
})
},
}
}
</script>
搜索页面,html部分
<template>
<view>
<view style="margin-top: 10rpx;">
<u-search margin="10rpx" placeholder="搜点什么..." v-model="keyword" clearabled @change="change"
@search="search"></u-search>
</view>
<view class="tag-item">
<view style="margin: 10rpx 6rpx;" v-for="(item,index) in listkeyword" :key="index">
<u-tag :text="item.goods_name" plain shape="circle" size="mini" @click="go_goods(item.id)"></u-tag>
</view>
</view>
<u-divider text="产品" textPosition="left"></u-divider>
<view>
<view class="goods_list">
<view class="goods_item" v-for="(item,index) in listkeyword" :key='item.id'
@click="go_goods(item.id)">
<image :src="item.img[0].url" mode="aspectFit"
style="border-radius: 10rpx;background-color: #fef0f0;"></image>
<view class="name">
<u--text size="16" :lines="1" :text="item.goods_name"></u--text>
</view>
<view class="prie">
<text class="text1">¥{{item.goods_present_price}}</text>
<text class="text2"
v-if="item.goods_market_price > 0">{{item.goods_market_price}}</text>
<text class="text3" v-if="item.goods_dingjin_price > 0">
<text style="font-size: 30rpx;">定金</text>{{item.goods_dingjin_price}}
</text>
</view>
</view>
</view>
<u-divider text="宝宝底线啦"></u-divider>
<view style="height: 20rpx;"></view>
</view>
</view>
</template>
搜索页面,css部分
<style lang="scss">
.tag-item{
display: flex;
flex-wrap: wrap;
}
.goods_list {
padding: 0 10rpx;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
padding: 15rpx;
.goods_item {
margin: 5rpx 0;
width: 355rpx;
border-radius: 15rpx;
box-shadow: 0px 0px 8px #cecece;
background-color: #ffffff;
image {
width: 300rpx;
height: 300rpx;
display: block;
margin: auto;
}
.prie {
font-size: 32rpx;
margin-bottom: 15rpx;
margin-left: 15rpx;
.text1 {
font-weight: bold;
color: #fb6583;
font-size: 35rpx;
width: 166px;
}
.text2 {
color: #606266;
font-size: 20rpx;
margin-left: 20rpx;
width: 166px;
text-decoration: line-through;
}
.text3 {
font-weight: bold;
color: #fb6583;
font-size: 35rpx;
margin-left: 20rpx;
width: 196px;
}
}
.name {
font-size: 30rpx;
line-height: 50rpx;
padding-bottom: 6rpx;
padding-top: 10rpx;
margin-left: 18rpx;
}
}
}
</style>
搜索页面,js部分,uni.$u.debounce(执行的函数,时间)
<script>
export default {
data() {
return {
keyword: '',
listkeyword: []//数据源
}
},
methods: {
change() {//当数据发生变化
let that = this
uni.$u.debounce(that.toNext, 600)
},
search() {//回车或者输入法的搜索
let that = this
uni.$u.debounce(that.toNext, 600)
},
toNext() {
//发起请求
}
}
}
</script>