vue手机商城项目实战(3)-首页搜索框组件实现

Web前端 潘老师 4年前 (2020-09-16) 3028 ℃ (0) 扫码查看

本文接上文:vue手机商城项目实战(2)-底部选项卡公共组件实现

1、在views目录下新建Search.vue代码如下:

<template>
    <div id="search">
        <!-- 顶部导航栏 -fixed 固定-->
        <mt-header fixed title="搜索">
            <mt-button slot="left" icon="back" @click="goback">返回</mt-button>
        </mt-header>
        <!-- 搜索框 -->
        <mt-search v-model="value" autofocus placeholder=" 请输入您想找的商品..." @keyup.enter.native="search" >
            <!-- 这里显示搜索结果-后期会引入商品列表 -->
        </mt-search>
    </div>
</template>

<script>
    import { MessageBox } from 'mint-ui'
    export default {
        data(){
            return {
                value:''
            }
        },
        methods:{
            // 返回
            goback(){
                this.$router.go(-1);
            },
            // 这里调用搜索方法-这里目前先简化为弹窗
            search(){
                MessageBox("提示","搜索内容为:"+this.value);
            }
        }
    }
</script>

<style scoped>
#search{
    position: fixed;
    width:100%;
}
.mint-search{
    margin-top: 40px;
}
/* /deep/可以改变深层组件样式但不会影响组件本身 */
/deep/ .mint-searchbar-inner{
    border-radius: 18px;
}
/deep/ .mint-searchbar{
    background-color: #eee;
}
</style>

我们把搜索框一按钮的形式引入到首页顶部导航栏右侧,点击跳转到搜索页,修改Home.vue代码如下:

<template>
    <div id="home">
        <!-- 顶部导航栏-fixed 固定 -->
        <mt-header fixed title="首页">
            <mt-button icon="search" slot="right" @click="search"></mt-button>
        </mt-header>
    </div>
</template>

<script>
    export default {
        methods:{
            // 点击搜索按钮,跳转到搜索组件
            search(){
                this.$router.push({name:"Search"});
            }
        }
    }
</script>

<style scoped>

</style>

router\index.js新增搜索组件路由配置如下:

{
    name:"Search",
    path:"/search",
    component:()=>import('@/views/Search.vue')
}

vue手机商城项目实战(3)-首页搜索框组件实现


版权声明:本站文章,如无说明,均为本站原创,转载请注明文章来源。如有侵权,请联系博主删除。
本文链接:https://www.panziye.com/java/web/1249.html
喜欢 (4)
请潘老师喝杯Coffee吧!】
分享 (0)
用户头像
发表我的评论
取消评论
表情 贴图 签到 代码

Hi,您需要填写昵称和邮箱!

  • 昵称【必填】
  • 邮箱【必填】
  • 网址【可选】