本文全面解析官方+高星第三方组件库,附每个组件的实战案例代码
scroll-view
- 滚动视图<scroll-view scroll-y style="height: 300rpx;" bindscrolltolower="loadMore">
<view wx:for="{{list}}" wx:key="id" class="item">
商品{{item.id}} - {{item.name}}
view>
<view wx:if="{{loading}}" class="loading">加载中...view>
scroll-view>
核心属性:
scroll-y
:允许垂直滚动scroll-top
:设置滚动位置bindscrolltolower
:触底事件form
- 表单容器<form bindsubmit="submitForm">
<input name="username" placeholder="输入用户名" />
<input name="password" password placeholder="输入密码" />
<button form-type="submit">登录button>
form>
Page({
submitForm(e) {
console.log('表单数据:', e.detail.value)
// {username: "test", password: "123456"}
}
})
picker
- 选择器日期选择模式:
<picker mode="date" value="{{date}}" bindchange="dateChange">
<view>当前日期: {{date}}view>
picker>
Page({
data: { date: '2025-06-01' },
dateChange(e) {
this.setData({ date: e.detail.value })
}
})
image
- 图片容器<image src="{{imgUrl}}" mode="aspectFill" lazy-load
bindload="imageLoaded" binderror="imageError" />
关键属性:
mode
:9种裁剪模式(常用aspectFit
/aspectFill
)lazy-load
:懒加载(页面滚动时加载)video
- 视频播放器<video src="https://example.com/video.mp4"
controls autoplay loop
bindplay="onPlay" bindended="onEnded" />
GitHub 25k+ | 安装:
npm i @vant/weapp
van-tabbar
- 底部导航<van-tabbar active="{{ active }}" bind:change="onChange">
<van-tabbar-item icon="home-o">首页van-tabbar-item>
<van-tabbar-item icon="shopping-cart-o">购物车van-tabbar-item>
van-tabbar>
Page({
data: { active: 0 },
onChange(e) {
this.setData({ active: e.detail })
wx.switchTab({ url: tabList[e.detail].path })
}
})
van-grid
- 宫格布局<van-grid column-num="3">
<van-grid-item
wx:for="{{icons}}"
wx:key="id"
icon="{{item.icon}}"
text="{{item.name}}"
bind:click="selectItem"
data-id="{{item.id}}"
/>
van-grid>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-svzGVZsf-1749012990235)(https://img.yzcdn.cn/vant/grid-demo.png)]
GitHub 6.2k+
wux-input
- 增强输入框<wux-input
label="手机号"
placeholder="请输入手机号"
value="{{ phone }}"
bind:change="onPhoneChange"
clearable
type="number"
maxlength="11"
rule="{{ { required: true, pattern: /^1[3-9]\d{9}$/ } }}"
/>
核心功能:
clearable
)GitHub 1.8k+
<animate
animation="{{bounceIn}}"
bind:click="startAnimation"
>
<view class="box">点击触发动画view>
animate>
const animate = require('@o2team/wxapp-animate').default
Page({
data: {
bounceIn: animate.bounceIn({ duration: 800 })
},
startAnimation() {
this.setData({
bounceIn: animate.bounceIn({ duration: 800 }).export()
})
}
})
支持动画类型:
// components/my-card.js
Component({
properties: {
title: { type: String, value: '默认标题' },
showAction: { type: Boolean, value: true }
},
data: { collapsed: false },
methods: {
toggleCollapse() {
this.setData({ collapsed: !this.data.collapsed })
this.triggerEvent('collapse', { state: this.data.collapsed })
}
}
})
<view class="card">
<view class="header">
<text>{{title}}text>
<view wx:if="{{showAction}}" bind:tap="toggleCollapse">
{{collapsed ? '展开' : '收起'}}
view>
view>
<view class="body" wx:if="{{!collapsed}}">
<slot>slot>
view>
view>
// components/my-card.json
{
"component": true,
"usingComponents": {},
"styleIsolation": "apply-shared" // 可选值: isolated/apply-shared/shared
}
// app.json
"usingComponents": {
"van-button": "@vant/weapp/button/index",
"wux-input": "wux-weapp/input/index"
}
<recycle-view
wx:for="{{list}}"
height="800"
item-height="100"
>
<view class="item">
<text>{{item.title}}text>
view>
recycle-view>
推荐库:
组件使用总结:
场景 | 推荐组件库 | 优势 |
---|---|---|
基础业务开发 | 官方WeUI | 原生支持,无需额外安装 |
电商类项目 | Vant Weapp | 丰富商城组件(商品卡片/优惠券) |
企业后台系统 | Wux Weapp | 专业级表单/数据表格 |
高互动页面 | Animate Weapp | 60+种交互动画效果 |
版权声明:本文代码示例采用MIT协议,转载请注明出处。商业使用请遵守组件库官方许可。