pages/wxml/index above the line pages/index/index and save file. After the simulator is refreshed automatically, you can find the pages/wxml/index.WXML file in the editor. This document describes how to modify this file. .wxml. Open the pages/wxml/index.WXML file and you will find that the WXML statements are similar to the HTML syntax if you are familiar with the HTML coding.<!--pages/wxml/index.wxml--><text>pages/wxml/index.wxml</text>
<!-- Add comment here--><Tag name Attribute name 1="Attribute value 1" Attribute name 2="Attribute value 2" ...> ...</Tag name>
<text>hello world<!--text is not closed, resulting in a compile error:VM148:2 ./pages/wxml/index.wxmlend tag missing, near text> 1 | <text>hello world| ^-->
<!-- A simple text tag--><text>hello world</text><!-- view contains text tags --><view><text>hello world</text></view>
<image class="userinfo-avatar" src="./image/a.png" ></image>
<!--pages/wxml/index.wxml--><text>Current time: {{time}}</text>
time: in the data curly brackets (new Date()).toString(),as shown below:// pages/wxml/index.jsPage({/*** Initial page data*/data: {time: (new Date()).toString()},})
<!--{time: (new Date()).toString()}--><text> Current time: {{time}}/text>
<!-- Correct coding --><text data-test="{{test}}"> hello world</text><!-- Wrong coding --><text data-test={{test}}> hello world </text >
<!--{w: 'w',W: 'W'}--><view>{{w}}</view><view>{{W}}</view><!-- OutputwW-->
<!--{var2: undefined,var3: null,var4: "var4"}--><view>{{var1}}</view><view>{{var2}}</view><view>{{var3}}</view><view>{{var4}}</view><!--Output:nullvar4-->
<!-- Display page content conditionally based on whether a is equal to 10 or not --><text>{{ a === 10? "variable a is equal to 10": "variable a is not equal to 10"}}</text>
<!--{ a: 1, b: 2, c: 3 }--><view> {{a + b}} + {{c}} + d </view><!-- Output 3 + 3 + d -->
<!--{ name: 'world' }--><view>{{"hello " + name}}</view><!-- Output hello world -->
<text>{{[1,2,3]}}</text><!-- Output 1,2,3 --><text>{{"hello world"}}</text><!-- Output hello world -->
wx:if="{{condition}}" to determine whether or not to render this code block: <view wx:if="{{condition}}"> True </view>
<view wx:if="{{length > 5}}"> 1 </view><view wx:elif="{{length > 2}}"> 2 </view><view wx:else> 3 </view>
wx:if is a control attribute, you must add it to a tag. To judge multiple component tags at once, you can use a <block/> tag to package multiple components together and use the wx:if control attribute above.<block wx:if="{{true}}"><view> view1 </view><view> view2 </view></block>
wx:for control attribute, you can use the data of the array items to re-render this component. The subscript variable name of the current item of the default array defaults to index, and the variable name of the current item of the array defaults to item: <!-- `array` is an array --><view wx:for="{{array}}">{{index}}: {{item.message}}</view><!-- Corresponding script filePage({data: {array: [{message: 'foo',}, {message: 'bar'}]}})-->
wx:for-item to specify the variable name of the array's current element and use wx:for-index to specify the variable name of the array's current subscript:<view wx:for="{{array}}" wx:for-index="idx" wx:for-item="itemName">{{idx}}: {{itemName.message}}</view>
block wx:if, you can use wx:for on a <block/> tag to render a structural block containing multiple nodes: <block wx:for="{{[1, 2, 3]}}"><view> {{index}}: </view><view> {{item}} </view></block>
wx:key to specify the unique identifiers of the items in the list.
The wx:key value is provided in either of the following formats:this: Represents the item itself in the for loop. This indicates that the item itself is a unique string or number. For example: <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 Number to the front </button>
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.lengthfor (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.lengththis.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})}})
name attribute to specify the name of the template. Then define that code snippet inside <template/>: <template name="msgItem"><view><text> {{index}}: {{msg}} </text><text> Time: {{time}} </text></view></template>
is attribute to declare the template to be used. Then, pass the data needed by the template. For example:<!--item: {index: 0,msg: 'this is a template',time: '2016-06-18'}--><template name="msgItem"><view><text> {{index}}: {{msg}} </text><text> Time: {{time}} </text></view></template><template is="msgItem" data="{{...item}}"/><!-- Output0: this is a template Time: 2016-06-18-->
is to dynamically decide which template to be rendered, as shown below: <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><!-- Outputoddevenoddevenodd-->
import and include.import can use the template defined by the target file in the current file, for example, a template named item is defined in item.wxml: <!-- item.wxml --><template name="item"><text>{{text}}</text></template>
item.wxml in index.wxml, you can use the item template:<import src="item.wxml"/><template is="item" data="{{text: 'forbar'}}"/>
import references. This means that the reference will import the template defined in the target file but not the template imported by the target file.
For example, C references B and B references A, and the template defined by B can be used in C, and the template defined by A can be used in B, but C cannot use the template defined by A, as shown below:<!-- A.wxml --><template name="A"><text> A template </text></template>
<!-- B.wxml --><import src="a.wxml"/><template name="B"> <text> B template </text></template>
<!-- C.wxml --><import src="b.wxml"/><template is="A"/> <!-- An alarm will be triggered, as template A is not defined in b --><template is="B"/>
include can introduce the entire code excluding <template/> <wxs/>. This is equivalent to copying the code to the location of include. For example: <!-- index.wxml --><include src="header.wxml"/><view> body </view><include src="footer.wxml"/>
<!-- header.wxml --><view> header </view>
<!-- footer.wxml --><view> footer </view>
Attribute | Type | Definition | Description |
id | String | Unique identifier of the component | Unique for entire page |
class | String | Style class for the component | The style class defined in the corresponding WXSS |
style | String | Inline style of the component | The inline style that can be set dynamically |
hidden | Boolean | Whether the component is displayed | Default display for all components |
data-* | Any | Custom attribute | When an event is triggered on the component, it will be sent to the event handler function. |
bind*/catch* | EventHandler | Component Event | - |
Feedback