微信小程序练习demo
程序员文章站
2022-03-14 13:51:50
...
https://www.w3cschool.cn/weixinapp/wgt21q8k.html
数据绑定demo1
<!-- index.wxml -->
<view wx:if="{{flag? true:false}}" class="item-{{0}}">
数据绑定:{{msg}}
<view>例如数组:{{object.key}}-{{object.array[0]}}</view>
<view wx:for="{{object.array}}">
{{object.key}}-{{item}}
</view>
</view>
/* index.wxss */
.item-0{
color: pink;
text-align: center;
}
// index.js
Page({
data: {
msg:"开始:",
id:0,
flag:true,
object:{
key:"testKey",
array:[1,2,3,4,5]
}
}
})
列表渲染dome2
<!-- index.wxml -->
<view wx:for="{{array}}">
{{index}}: {{item.message}}
</view>
<!--
默认下标:index
默认变量名:item
wx:for-index: 赋值下标名
wx:for-item: 赋值bo名-->
<view wx:for="{{array}}" wx:for-index="idx" wx:for-item="itemName">
{{idx}}: {{itemName.message}}
</view>
<!-- <view>99乘法表:</view>
<view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="i">
<view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="j">
<view wx:if="{{i <= j}}">
{{i}} * {{j}} = {{i * j}}
</view>
</view>
</view> -->
<block wx:for="{{[1, 2, 3]}}">
<view> {{index}}:{{item}} </view>
</block>
<switch wx:for="{{objectArray}}" wx:key="unique" style="display: block;"> {{item.id}} </switch>
<button bindtap="switch"> Switch </button>
<button bindtap="addToFront"> Add to the front </button>
<switch wx:for="{{numberArray}}" wx:key="*this" style="display: block;"> {{item}} </switch>
<button bindtap="addNumberToFront"> Add to the front </button>
// index.js
Page({
data: {
array: [{
message: 'foo',
}, {
message: 'bar'
}],
objectArray: [
{ id: 5, unique: 'unique_5' },
{ id: 4, unique: 'unique_4' },
{ id: 3, unique: 'unique_3' },
{ id: 2, unique: 'unique_2' },
{ id: 1, unique: 'unique_1' },
{ id: 0, unique: 'unique_0' },
],
numberArray: [1, 2, 3, 4]
},
switch: function (e) {
const length = this.data.objectArray.length
for (let i = 0; i < length; ++i) {
const x = Math.floor(Math.random() * length)
const y = Math.floor(Math.random() * length)
const temp = this.data.objectArray[x]
this.data.objectArray[x] = this.data.objectArray[y]
this.data.objectArray[y] = temp
}
this.setData({
objectArray: this.data.objectArray
})
},
addToFront: function (e) {
const length = this.data.objectArray.length
this.data.objectArray = [{ id: length, unique: 'unique_' + length }].concat(this.data.objectArray)
this.setData({
objectArray: this.data.objectArray
})
},
addNumberToFront: function (e) {
this.data.numberArray = [this.data.numberArray.length + 1].concat(this.data.numberArray)
this.setData({
numberArray: this.data.numberArray
})
}
})
条件渲染dome3
<!-- index.wxml -->
<view wx:if="{{strLength == 1 ? true : false}}">True</view>
<view wx:if="{{strLength > 5}}"> 1 </view>
<view wx:elif="{{strLength > 2}}"> 2 </view>
<view wx:else> 3 </view>
<block wx:if="{{true}}">
<view> view1 </view>
<view> view2 </view>
</block>
// index.js
Page({
data: {
strLength:Math.floor(Math.random()*8)
}
})
模板dome4
<!-- index.wxml
定义模板<template/>
可以在其他页面定义 -->
<template name="msgItem">
<view class='item-0'>
<view>{{index}}:{{msg}}</view>
<view>Time:{{time}}</view>
</view>
</template>
<!-- 引用模板 is是引用
data用...展开数据是是给定义模板用的 -->
<template is="msgItem" data="{{...item}}" />
<!-- is属性使用Mustache语法动态渲染模板 -->
<template name="odd">
<view>odd</view>
</template>
<template name="even">
<view>even</view>
</template>
<block wx:for="{{[1,2,3,4,5]}}">
<template is="{{item % 2 == 0 ? 'even' : 'odd'}}"/>
</block>
/* index.wxss */
.item-0{
color: pink;
text-align: center;
}
// index.js
Page({
data: {
item:{
index:0,
msg:'this is a template',
time:'2016-09-15'
}
}
})
上一篇: Java微信小程序支付Demo
下一篇: 行为型模式---状态模式