【微信小程序】使⽤⾃定义组件实现头部搜索框

本文介绍如何在微信小程序中创建并使用自定义组件SearchInput,详细阐述了从创建组件文件、声明组件、引入组件到在页面中实际应用的完整过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使⽤⾃定义组件实现头部搜索框
在这里插入图片描述
1. 创建自定义组件
在⽂件夹components/SearchInput内,右键新建Component名为SearchInput
在这里插入图片描述
在组件的json⽂件中进⾏⾃定义组件声明
components/SearchInput.json

{
  "component": true,
  "usingComponents": {}
}

在组件的wxml⽂件中编写组件模板
components/SearchInput.wxml

<view class="search_input">
  <navigator url="/pages/search/index" open-type="navigate">搜索</navigator>
</view>

在组件的wxss⽂件中加⼊组件样式
components/SearchInput.wxss

.search_input {
  height: 90rpx;
  padding: 10rpx;
  background-color: var(--themeColor);}
.search_input navigator {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #fff;
  border-radius: 15rpx;
  color: #666;
}

2. 声明引⼊⾃定义组件
在⻚⾯的json⽂件中进⾏引⽤声明,提供对应的组件名组件相对路径
pages/index/index.json

{
  // 引用声明
  "usingComponents": {
    // 组件名: 组件相对路径
    "SearchInput": "../../components/SearchInput/SearchInput"
  },      
  "navigationBarTitleText": "商品分类"
}

3.⻚⾯中使⽤⾃定义组件
在页面的wxml文件中使用自定义组件
pages/index/index.json

<view class="pyg_index">
  <!-- 搜索框开始 -->
  <SearchInput></SearchInput>
  <!-- 搜索框结束 -->
</view>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值