Conditional rendering on The View layer of the WeChat mini-app tutorial series (10), mini-app View

Source: Internet
Author: User

Conditional rendering of the view layer of the mini-program tutorial series (10), mini-program View

This tutorial will share with you how to use wx: if to perform conditional rendering on The View layer for your reference. The specific content is as follows:


Conditional rendering of the view layer using wx: if

Example:

Wxml: view

<! -- Index. wxml --> <button bindtap = "EventHandle"> button </button> <! -- Wx: if --> <view wx: if = "{boolean = true }}"> <view class =" bg_black "> </view> <view wx: elif = "{boolean = false }}"> <view class =" bg_red "> </view>

Wxss:

/**index.wxss**/.bg_black { height: 200rpx; background: lightskyblue;}.bg_red { height: 200rpx; background: lightpink;}

Js:

// index.jsPage({ data: {  boolean:false }, EventHandle: function(){  var bol = this.data.boolean;  this.setData({   boolean: !bol  }) }})

Run:


Continued:

Change the Green view to block.

Wxml: block

<! -- Index. wxml --> <button bindtap = "EventHandle"> button </button> <! -- Wx: if --> <block wx: if = "{boolean = true }}"> <view class =" bg_black "> </view> </block> <block wx: elif = "{boolean = false }}"> <view class =" bg_red "> </view> </block>

Run:


Continued:

Add a wx: for list rendering.

Wxml:

<! -- Index. wxml --> <button bindtap = "EventHandle"> button </button> <! -- Wx: if --> <block wx: if = "{boolean = true}" wx: for = "{arr }}"> <view class =" bg_black "> content: {item }}</view> </block> <block wx: elif = "{boolean = false }}"> <view class =" bg_red "> NO content </view> </block>

Index. js:

// index.jsPage({ data: {  boolean:false,  arr: [1,2,3] }, EventHandle: function(){  var bol = this.data.boolean;  this.setData({   boolean: !bol  }) }})

Run:

Editing error.


The reason is that wx: if cannot be shared with wx:!

Continued:

Wx: if and wx: for must be used separately

Wxml:

<! -- Index. wxml --> <button bindtap = "EventHandle"> button </button> <! -- Wx: if --> <block wx: if = "{boolean = true}"> <block wx: for = "{arr }}"> <view class =" bg_black "> content: {item }}</view> </block> <block wx: elif = "{boolean = false }}"> <view class =" bg_red "> NO content </view> </block>

Wxss:

/**index.wxss**/.bg_black { height: 200rpx; background: lightskyblue;}.bg_red { height: 200rpx; background: lightpink;}

Index. js:

// index.jsPage({ data: {  boolean:false,  arr: [1,2,3] }, EventHandle: function(){  var bol = this.data.boolean;  this.setData({   boolean: !bol  }) }})

Run:


The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.