文章目录
数据绑定条件渲染列表渲染模板wxml文件引用共同属性
数据绑定
<view >
<text >hello,
</text>
<text data-title="{{title}}">{{name}}
</text>
<view >{{"welcome " + name}}
</view>
</view>
Page({
data
:{
name
:"Nicholas",
title
:"名字"
}
})
条件渲染
wx:if="{{}}" | wx:else
<view >
<view wx:if="{{num>50}}">{{num}}大于50
</view>
<view wx:else>{{num}}小于等于50
</view>
</view>
Page({
data
:{
num
:Math
.floor(Math
.random()*100+1)
}
})
wx:if="{{}}" | wx:elif="{{}}" | wx:else
<view >
<view wx:if="{{num<20}}">{{num}}小于20
</view>
<view wx:elif="{{num>20 && num<80}}">{{num}}大于20且小于80
</view>
<view wx:else>{{num}}大于等于80
</view>
</view>
Page({
data
:{
num
:Math
.floor(Math
.random()*100+1)
}
})
多个元素用<block>包裹
<block wx:if="{{num>10}}">
<view >num是{{num}}
</view>
<view >num大于10为真
</view>
</block>
Page({
data
:{
num
:Math
.floor(Math
.random()*100)
}
})
列表渲染
index | item
<view wx:for="{{todos}}">
{{index}}-{{item.title}}
</view>
Page({
data
:{
todos
:[
{title
:"吃饭"},
{title
:"睡觉"},
{title
:"打豆豆"}
]
}
})
其中,数组下标默认是index,数组项默认是item。
自定义变量名,idx | todoItem
<view wx:for="{{todos}}" wx:for-index="idx" wx:for-item="todoItem">
{{idx}}-{{todoItem.title}}
</view>
wx:for-index="idx",index指定为idx;wx:for-item="todoItem",item指定为todoItem。
wx:key="title" 数组中的元素是对象, wx:key的值是对象的一个属性,且该属性值是字符串或数值。
<switch wx:for="{{todos}}" wx:key="title" >{{item.title}}
</switch>
<button bindtap="addTodoItem">Add
</button>
Page({
data
:{
todos
:[
{title
:"吃饭"},
{title
:"睡觉"},
{title
:"打豆豆"}
]
},
addTodoItem
:function(){
const newTodoItem
= {
title
:"需求开发"
};
this.data
.todos
= [newTodoItem
].concat(this.data
.todos
);
this.setData({
todos
:this.data
.todos
})
}
})
wx:key用来唯一标识列表中的每项,和React里key的性质差不多。 当需要在列表最前面添加新项时,小程序只会创建新项,列表中的已有项则不会经历重建过程,只会重新排序。
wx:key="*this" 数组中的元素是字符串或数值,this代表了元素自身。
<switch wx:for="{{words}}" wx:for-item="word" wx:key="*this" >{{word}}
</switch>
<button bindtap="AddWord">Add
</button>
Page({
data
:{
words
:["foo","bar"]
},
AddWord
:function(){
this.data
.words
= ["hello"].concat(this.data
.words
);
this.setData({
words
:this.data
.words
})
}
})
模板
定义模板 <template name="">,name属性唯一标识该模板
<template name="item">
<view>from:{{from}}
</view>
<view >msg:{{msg}}
</view>
</template>
导入模板 <import src="" />使用模板 <template is="" data="">,is属性决定渲染哪个模板,data是传入模板的数据
<import src="./item.wxml" />
<template is="item" data="{{...item}}"></template>
Page({
data
:{
item
:{
from:"Nicholas",
msg
:"Nice to meet you"
}
}
})
wxml文件引用
<import src=""/> import是有作用域的。
<template name="A">
<view>This is A template
</view>
</template>
<import src="./a.wxml"/>
<template name="B">
<text>This is B template
</text>
</template>
<import src="./b.wxml" />
<template is="A"></template>
<template is="B"></template>
index.wxml引入b.wxml,b.wxml引入a.wxml,简单描述成:index->b->a,但index.wxml中无法使用a.wxml中定义的模板。
<include src=""> include可以引入目标文件中除了<template>、<wxs>的所有代码。
<template name="A">
<view>This is A template
</view>
</template>
<view>view from A
</view>
<import src="./a.wxml"/>
<template name="B">
<text>This is B template
</text>
</template>
<view>view from B
</view>
<include src="./a.wxml"/>
<include src="./b.wxml"/>
<view>this is header
</view>
<view>this is footer
</view>
<include src="./header.wxml"/>
<view>this is content
</view>
<include src="./footer.wxml"/>
共同属性
id|style|class
<view>
<text id="title" class="title" style="color:red">world
</text>
</view>
hidden
<view>
<text hidden="{{isHidden}}">hello,
</text>
<text id="title" class="title" style="color:red">world
</text>
</view>
#title{
font-family:simsun
;
}
.title{
font-size:14px
;
}
Page({
data
:{
isHidden
:false
}
})
data-title|bindtap
<view>
<text data-title="helloworld" bindtap="handleClick">hello,world
</text>
</view>
Page({
handleClick
:function(e
){
console
.log(e
);
console
.log(e
.target
.dataset
["title"])
}
})