前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >uni-app实现搜索跳转功能

uni-app实现搜索跳转功能

作者头像
切图仔
发布2022-09-08 15:59:08
2.5K0
发布2022-09-08 15:59:08
举报
文章被收录于专栏:生如夏花绚烂生如夏花绚烂

功能描述:点击导航栏跳转到搜索页,并在搜索页完成数据检索,数据渲染

1.监听index.vue的搜索框

代码语言:javascript
复制
//监听点击导航栏搜索框
onNavigationBarSearchInputClicked(){
    //跳转搜索页面
    uni.navigateTo({
        url:'../search/search'
    })
},

2.新键搜索页面search.vue

代码语言:javascript
复制
<template>
    <view>
        <template v-if="searchList.length===0">
            <!-- 搜索历史 -->
            <view class="py-2 font-md px-2">搜索历史</view>
            <view class="flex flex-wrap">
            <!-- 历史记录 -->
                <view class="border rounded font mx-2 my-10 px-2"
                v-for="item,index in list" 
                :key="index"
                hover-class="bg-light"
                @click="clickSearchHistory(item)">{{item}}</view>
            </view>
        </template>
        <!-- 数据列表 -->
        <block v-for="item,index in searchList" :key="index">
            <commonList :item="item" :index="index"></commonList>
        </block>
    </view>
</template>

我们在历史记录元素添加了click事件,这个事件触发历史记录函数完成历史搜索,下文展示。

3.配置pages.json

代码语言:javascript
复制
{
    "path" : "pages/search/search",
    "style" : {
    //导航配置
    "app-plus":{
        "titleNView":{
            //搜索框
            "searchInput":{
                "align":"center",
                "backgroundColor":"#f5f4f2",
                "borderRadius":"4px",
                "disabled":false,
                "placeholder":"搜索帖子"
            },
            "buttons":[
                {
                    "color":"#333333",
                    "colorPressed":"#fd597c",
                    "float":"right",
                    "fontSize":"14px",
                    "text":"搜索"
                }

            ]
        }
    }
    }
}

搜索相关

代码语言:javascript
复制
<script>
    import demo from '@/newsdata.js'//导入新闻数据
    import commonList from "@/components/common/common-list.vue"
    export default {
        components:{commonList},
        data() {
            return {
                searchText:"",
                list:['测试历史','211321','程序员']
                //搜索结果
                searchList:[]
                
            }
        },
        //监听导航输入
        onNavigationBarSearchInputChanged(e){
            // console.log(e)
            this.searchText=e.text
        },
        //监听导航搜索按钮
        onNavigationBarButtonTap(e){
            //如果索引==0表示搜索按钮点击
            if(e.index==0)this.searchEvent()
            
        },
        methods: {
            //点击搜索历史
            clickSearchHistory(text){
                this.searchText = text//获取历史记录
                this.searchEvent()//调用搜索事件
            },
            searchEvent(){
                //收起键盘
                uni.hideKeyboard()
                // //请求搜索
                //loading
                uni.showLoading({
                    title: '加载中...',
                    mask: false
                });
                setTimeout(()=>{
                    this.searchList = demo
                    //隐藏loading
                    uni.hideLoading()
                },3000)
                
                console.log(this.searchList)
            }
        }
    }
</script>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-07-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档