基本组件
view
属性名 |
类型 |
默认值 |
说明 |
hover-class |
String |
none |
指定按下去的样式类。当hover-class="none" 时,没有点击态效果 |
1
| <view hover-class="hover_class">index</view>
|
1 2 3 4
| .hover_class{ background-color: aqua; opacity: .5; }
|

text
属性名 |
类型 |
默认值 |
说明 |
selectable |
Boolean |
false |
文本是否可选 |
decode |
Boolean |
false |
是否解码 |
1 2 3 4
| <view> <text selectable>index文本</text> <text decode>普 通</text> </view>
|

image
- 图片标签,
image
组件默认宽度320px、高度240px
注意:该标签其实是web中的图片标签和背景图片的结合! ! !并且不支持以前的web中的背景图片的写法! ! !
属性名 |
类型 |
默认值 |
说明 |
src |
String |
|
图片资源地址,支持云文件ID (2.2.3起) |
mode |
String |
scaleToFlil |
图片裁剪、缩放的模式 |
lazy-load |
Boolean |
false |
图片懒加载。只针对page与scoll-view下的image有效 |
1 2 3 4
| <view>
<image src="../../image/headerimg.png" mode="aspectFill"></image> </view>
|
swiper
- 滑块视图容器。其中只可放置
swiper-item
组件,否则会导致未定义的行为
swiper
默认宽度100%,高度150px。swiper-item
默认宽度和高度都为100%
属性名 |
类型 |
默认值 |
说明 |
indicator-dots |
Boolean |
false |
是否显示面板指示点 |
indicator-color |
Color |
rgba(0.0.0.3)指示点颜色 |
|
indicator-active-color |
Color |
000000 |
当前选中的指示点颜色 |
autoplay |
Boolean |
false |
是否自动切换 |
interval |
Number |
5000 |
自动切换时间问隔 |
circular |
Boolean |
false |
是否采用衔按滑动 |
1 2 3 4 5 6
| <swiper autoplay indicator-dots indicator-color="white" indicator-active-color="red" circular interval="2000"> <swiper-item><image mode="widthFix" src="../../image/headerimg.png"></image></swiper-item> <swiper-item><image mode="widthFix" src="../../image/headerimg.png"></image></swiper-item> <swiper-item><image mode="widthFix" src="../../image/headerimg.png"></image></swiper-item> <swiper-item><image mode="widthFix" src="../../image/headerimg.png"></image></swiper-item> </swiper>
|

navigator
1 2 3 4 5 6 7 8
| <navigator url="../index/index" open-type="navigate">跳到首页index</navigator>
<navigator url="../index/index" open-type="redirect">跳到首页index</navigator>
<navigator url="../index/index" open-type="switchTab">跳到首页index</navigator>
<navigator url="../index/index" open-type="reLaunch">跳到首页index</navigator>
|
自定义组件
创建组件
- 自定义组件类似于页面,一个自定义组件由
json wxml wxss js
4个文件组成
- 在项目根目录创建
componets
文件夹,再创建组件MyHeader
文件夹,然后右击点击【新建Component】即可生成json wxml wxss js
4个文件
- 要编写一个自定义组件,首先需要在
json
文件中进行自定义组件声明(将 component
字段设为 true
可将这一组文件设为自定义组件):
1 2 3 4 5 6
| <view class="my_Header"> {{cData}} <view> <slot> </slot> </view> </view>
|
1 2 3 4 5 6 7 8 9
| Component({ properties: { cData:{ value:"组件的默认值", type:String } } })
|
1 2 3 4
| .my_Header{ font-size: 50px; background-color: aqua; }
|
使用组件
1 2 3 4 5
| { "usingComponents": { "MyHeader": "../../components/MyHeader/MyHeader" } }
|
- 在
index.wxml
中使用组件标签,标签名为上面设置的MyHeader
组件名
1 2 3
| <view> <MyHeader>页面</MyHeader> </view>
|

事件绑定
1 2
| <view bindtap="handleTap" data-id="1"></view>
|
1 2 3 4 5 6 7 8 9
| handleTap(event){ const id = event.currentTarget.dataset.id console.log(event) this.setData({ key : value }) }
|
1
| <view catchtap="handleTap"></view>
|