Small Program Foundation 11: Conditional rendering

Source: Internet
Author: User

1. Conditional Rendering Wx:if

In the framework, we use wx:if= "{{condition}}" to determine whether the code block needs to be rendered

<wx:if= "{{condition}}"></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, it needs to be added to a label. If we want to judge multiple component labels at once, we can use a </block> to wrap multiple components and use wx:if on the block

<wx:if= "{{true}}">  <view> </ View >  < View > </ View > </ Block >

Note: <block/> not a component, it is just a wrapper element, does not make any rendering in the page, only accepts the control attribute.

Wx:if vs Hidden

Because templates in Wx:if can also contain data bindings, all when the WX:IF's conditional value is switched, the framework has a partial rendering process, because he ensures that conditions are quickly destroyed or re-rendered when switching.

At the same time Wx:if is also inert, if the initial render condition is false, the framework does nothing to start the partial rendering when the condition is first true.

In contrast hidden is much simpler, components are always rendered, but simple controls are displayed or hidden.

In general: Wx:if has higher switching consumption and hidden has higher initial rendering consumption. Therefore, if it is necessary to switch frequently, it would be better to use hidden and wx:if would be better if the conditions are not likely to change at runtime.

Small Program Foundation 11: Conditional rendering

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.