使用HBuilder uni-app VUE 创建微信小程序中 自定义搜索组件点击跳转页面

实现搜索框点击跳转

 

创建自定义 my-search UI组件

                 1.在项目根目录的  uni_modules 目录上,鼠标右键,选择 新建组件,填写组件信息后,最后点击 创建 按钮:

 my-search 文件中代码 实现搜索框
<template>
  <!-- 搜索模块 -->
  <view class="my-search-container" :style="{ 'background-color' : bgcolor }">
    <!-- 搜索框 -->
    <view class="my-search-boc" :style="{ 'border-radius' : radius + 'px' }">
      <!-- 搜索框内图标  (使用 uni-ui 提供的图标组件) -->
      <uni-icons type="search" size="17"></uni-icons>
      <!-- 搜索框内文本 -->
      <text class="placeholder">搜索</text>
    </view>
  </view>
</template>

<script>
  export default {
    // 声明
    props: {
      // 背景颜色
      bgcolor: {
        type: String,
        default: '#C00000'
      },
      // 圆角尺寸
      radius: {
        type: Number,
        default: 18  // 单位px
      }
    },
    data() {
      return {
        
      }
    }
  }
</script>

<style lang="scss">
  // 搜索模块
  .my-search-container {
    height: 50px;
    // background-color: #C00000;
    display: flex;
    align-items: center;
    padding: 0 10px;
    // 搜索框
    .my-search-boc {
      height: 36px;
      background-color: #ffffff;
      // border-radius: 18px;
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      // 搜索内文本
      .placeholder {
        font-size: 15px;
        margin-left: 5px;
      }
    }
  }
</style>

在 Cate 分类页面 自定义搜索组件中 创建click点击事件 

<my-search @click="gotoSearch"></my-search>

 在 Cate文件 methods中 定义click点击事件  测试打印  'OK'

// 自定义搜索组件项点击跳转事件
      gotoSearch() {
        console.log('ok')
      }

my-search 文件 在搜索模块中添加click点击事件

<view class="my-search-container" :style="{ 'background-color' : bgcolor }" @click="searchBoxHandler"><view》

 在my-search文件  methods 定义点击事件   使用this.$emit 子传父 实现 Cate文件 定义 自定义搜索组件 click点击事件 打印测试 'OK'

// 搜索模块项点击跳转
      searchBoxHandler() {
        // Cate文件中 自定义搜索组件 向 当前文件 搜索模块 传值 (子向父传值)
        this.$emit('click')
      }

在subpug分包 新建uni-app页面 search 文件 

Cate文件 定义 自定义搜索组件 click点击事件跳转到search页面

 // 自定义搜索组件项点击跳转事件
      gotoSearch() {
        // 点击跳转到search 搜索详情页
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值