【weixin】【小程序】入门

<!--pages/wxml/index.wxml-->

<!--
<view>
<text>Hello Wechat mini World</text>
</view>
<view>
<image src="./image_one.png"></image>
</view>
<text>当前时间: {{time}}</text>
<view>{{w}}</view>
<view>{{W}}</view>
<view>{{undefined}}</view>
<view>{{null}}</view>
-->

<text>Hello Wechat World</text>
<!-- 栗子 三元运算 a=1 -->
<view>{{ a == 10? "变量 a 等于10": "变量 a 不等于10"}}</view>
<!-- 输出 变量 a 不等于10 -->

<!-- 栗子 算数运算 { a: 1, b: 2, c: 3 } -->
<view> {{ a + b }} + {{ c }} + d </view>
<!-- 输出 3 + 3 + d -->

<!-- 栗子 字符串拼接 {name: 'world'} -->
<view>{{ 'hello ' + name }}</view>
<!-- 输出 hello world -->

<!-- 栗子 常量 -->
<view>{{[1,2,3]}}</view>
<!-- 输出 1,2,3 -->


<!-- 栗子 逻辑判断 -->
<view wx:if="{{length > 5}}"> 1 </view>
<view wx:elif="{{length > 2}}"> 2 </view>
<view wx:else> 3 </view>
<!--length: 10 输出 1 -->
<!--length没定义 输出 3 -->

<!-- 栗子 逻辑判断 一次性判断多个组件标签-->
<block wx:if="{{false}}">
<view> view1 </view>
<view> view2 </view>
</block>
<!--无输出 -->
<block wx:if="{{true}}">
<view> view1 </view>
<view> view2 </view>
</block>
<!--输出
view1
view2
-->

<!-- 栗子 列表渲染 -->
<!-- 在组件上使用 wx:for 控制属性绑定一个数组,即可使用数组中各项的数据重复渲染该组件。默认数组的当前项的下标变量名默认为 index,数组当前项的变量名默认为 item -->
<!-- 对应的脚本文件
Page({
data: {
array: [{
message: 'foo',
}, {
message: 'bar'
}]
}
})
-->
<view wx:for="{{array}}">
{{index}}: {{item.message}}
</view>
<!-- 输出 0: foo -->

<!-- 栗子 列表渲染 指定数组当前元素和下标的变量名-->
<view wx:for="{{array}}" wx:for-index="idx" wx:for-item="itemName">
{{idx}}: {{itemName.message}}
</view>
<!-- 输出 0: foo -->

<!-- 栗子 列表渲染 <block/> -->
<block wx:for="{{[100,200,300]}}">
<view> {{index}}: </view>
<view> {{item}} </view>
</block>
<!--
0:
100
1:
200
2:
300
-->


<!-- 栗子 列表渲染 动态 -->
<!--
如果列表中项目的位置会动态改变或者有新的项目添加到列表中,并且希望列表中的项目保持自己的特征和状态(如 <input/> 中的输入内容, <switch/> 的选中状态),需要使用 wx:key 来指定列表中项目的唯一的标识符。

wx:key 的值以两种形式提供:

字符串,代表在 for 循环的 array 中 item 的某个 property,该 property 的值需要是列表中唯一的字符串或数字,且不能动态改变。

保留关键字 this 代表在 for 循环中的 item 本身,这种表示需要 item 本身是一个唯一的字符串或者数字,如:

当数据改变触发渲染层重新渲染的时候,会校正带有 key 的组件,框架会确保他们被重新排序,而不是重新创建,以确保使组件保持自身的状态,并且提高列表渲染时的效率。
-->
<!--
Page({
data: {
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
})
}
})
-->
<switch wx:for="{{objectArray}}" wx:key="unique">{{item.id}}</switch>
<button bindtap="switch"> Switch </button>
<button bindtap="addToFront"> Add to the front </button>

<switch wx:for="{{numberArray}}" wx:key="*this"> {{item}} </switch>
<button bindtap="addNumberToFront"> Add Numnber to the front </button>


<!-- 栗子 列表渲染 动态 -->




猜你喜欢

转载自www.cnblogs.com/suren2017/p/12382584.html