Conditional rendering for mini-program tutorials
Series of articles:
Mini-program tutorial-WXSS
References to mini-program tutorials
Events of mini-program tutorials
Small Program tutorial Template
List rendering of Applet tutorials
Conditional rendering of mini-program tutorials
Data Binding in applet tutorial
Mini-program tutorial-WXML
Wx: if
In MINA, we use wx: if = "{condition}" to determine whether to render the code block:
<View wx: if = "{condition}"> True </view>
You can also use wx: elif and wx: else to add an else block:
<view wx:if="{{length > 5}}"> 1 </view><view wx:elif="{{length > 2}}"> 2 </view><view wx:else> 3 </view>
Block wx: if
Because wx: if is a control attribute, you need to add it to a tag. However, if we want to determine multiple component labels at a time, we can use one <block/> label to wrap multiple components and use wx: if to control the attributes above.
<block wx:if="{{true}}"> <view> view1 </view> <view> view2 </view></block>
Note: <block/> is not a component. It is only a packaging element. It does not make any rendering on the page and only accepts control attributes.
Wx: if vs hidden
Because the template in wx: if may also contain data binding, MINA has a partial rendering process when the conditional values of wx: if are switched, it will make sure that the condition block is destroyed or re-rendered when switching.
At the same time, wx: if is also inert. if the initial rendering condition is false, MINA does nothing and starts partial rendering only when the condition is changed to true for the first time.
In contrast, hidden is much simpler, and components are always rendered, just simple control of display and hiding.
In general, wx: if has higher switching consumption, while hidden has higher initial rendering consumption. Therefore, if frequent switchover is required, it is better to use den. if the running condition is unlikely to change, wx: if is better.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!