当前位置:网站首页>[wechat applet] understand conditional rendering, list rendering and wxss template style
[wechat applet] understand conditional rendering, list rendering and wxss template style
2022-07-24 06:53:00 【Su liang.py】
Author's brief introduction : Su Liang ( Focus on web crawlers , Data analysis , On the way to learning the front end )
Blog home page : Su Liang .py The blog of
Series column : Basic course of applet development
Aphorisms : The sea is wide with fish , The sky is high and the birds are flying .
If you think the blogger's article is good , I hope you can support me for the third time in a row !!!
Follow the likes collection
Conditions apply colours to a drawing
wx:if
In order to display different information segments in the applet , We can show by conditional rendering , If the conditions are met, the page will be displayed , If you are not satisfied, hide .
coordination wx:elif and wx:else To use
grammar :
wx:if="{ {}}"
example :
Add three view Components
<view> male </view>
<view> Woman </view>
<view> Unknown </view>
stay js In the document data Define a data in 
by wxml Three of them view Component to add conditional rendering statements
<view wx:if="{
{gender == 1}}"> male </view>
<view wx:elif="{
{gender == 2}}"> Woman </view>
<view wx:else> Unknown </view>
At this time, it only shows that the conditions are met in the applet gender by 1 Of view Component .
combination block Use wx:if
If you want a one-time Control the display and hiding of multiple components , You can use one block Tags wrap multiple components , And in block Use on label wx:if Control properties .
<block wx:if="{
{gametype == 1}}">
<view> Hero alliance </view>
<view>QQ Flying car </view>
<view> Dungeons </view>
</block>
<block wx:else="{
{gametype == 2}}">
<view> Glory of Kings </view>
<view> Protogod </view>
<view> Game for Peace </view>
</block>
<block wx:else>
<view> other </view>
</block>

block Not a component , But a container of wrapping nature , It will not be rendered on the page .
hidden Hidden elements
As we said above, you can use wx:if To show and hide elements ,hidden You can also control the display and hiding of elements .
grammar :
hidden=”{ {}}“Condition is true Then hide , Otherwise, it will show .
<view hidden="{
{true}}"> Su Liang </view>
wx:if and hidden The difference between
1. The operation mode is different
wx:ifWith Dynamically create and remove The way of elements , Control the display and hiding of elementshiddenWith Switching styles The way ( display: none/block;), Control the display and hiding of elements
2. When to use ?
- Before we need Switch frequently When showing and hiding , It is recommended to use
hidden - When our conditions are complicated , It is recommended to use
wx:ifcoordinationwx:elifandwx:elseTo use .
The list of rendering
wx:for
adopt wx:for According to the specified array , Loop render duplicate component structures .
Use at the same time index To display the current index , Use item To display the current item
example :
<view wx:for="{
{arr1}}">
This is the first {
{index+1}} A character ,TA yes {
{item}}.
</view>

index Index entry from 0 Start
Manually specify the index and the variable name of the current item
- Use
wx:for-indexYou can specify the variable name of the index of the current circular item - Use
wx:for-itemYou can specify the variable name of the current item
example :
<view wx:for="{
{arr1}}" wx:for-index="num" wx:for-item="acter">
This is the first {
{num+1}} A character ,TA yes {
{acter}}.
</view>
wx:key Use
When the applet realizes list rendering , The suggestion is The rendered list item specifies a unique key value , So as to improve the efficiency of rendering .
example :
wxml:
<view wx:for="{
{arr2}}" wx:key="id">{
{item.name}}</view>
js:
If there is no setting in the list key value , Then we can also default index As our key value .
wxss Template style
wxss What is it?
wxss (weixin Style Sheets) It's a style language , To beautify WXML Component style , Similar to... In web development CSS.
wxss and css The relationship between
wXSS have CSS Most features , meanwhile ,WXSS Also on the CSS It has been expanded and modified , To adapt to the development of wechat applet .
And CSS comparison ,WXSS The extended features are :
- rpx Unit of measure
- @import Style import
meanwhile wxss and css There are general selectors and general style rules .
rpx Company
rpx ( responsive pixel) It is unique to wechat applet , Size unit used to solve screen adaptation .
rpx Realization principle
rpx The implementation principle of is very simple : Given the different screen sizes of different devices , In order to realize the automatic adaptation of the screen ,rpx Put all the devices on the screen , Divide equally in width 750 Share ( namely : The total width of the current screen is 750rpx).
- On smaller devices ,1rpx The width represented is small
- On larger devices ,1rpx The width represented is large
When applets run on different devices , Meeting Automatic handle rpx The style units are converted to the corresponding pixel units to render , So as to realize screen adaptation .
边栏推荐
- Directory and file management
- [jQuery自定义插件] 1 自定义缓存插件-jQueryCache
- MGR_mysqlsh_keepalive高可用架构部署文档
- Redis基本类型-有序集合Zset
- XXL execute node error log swiping
- Redis数据类型-列表List
- 中药材的鉴别
- [lvgl (5)] label usage
- An AI plays 41 games, and the comprehensive performance score of Google's latest multi game decision transformer is twice that of dqn
- Redis入门
猜你喜欢

Special effects - when the mouse moves, there will be a custom expression trail
![[esp8266 spot welder] Based on esp8266 for Arduino](/img/9a/81b0d16572116445d46ba4e34a6e4e.png)
[esp8266 spot welder] Based on esp8266 for Arduino
![[learning notes] possible reasons and optimization methods for white screen on Web pages](/img/80/152a2827b0e653ebf2365c16ce3b40.png)
[learning notes] possible reasons and optimization methods for white screen on Web pages
![[lvgl (4)] event and event bubble of the object](/img/1e/9fe3d34f5f8d8c7298a106dca8d949.png)
[lvgl (4)] event and event bubble of the object

Sparksql core usage, 220724,
![[lvgl (2)]](/img/f8/d04183cf74896295382765a9dfd88d.png)
[lvgl (2)]

Mac解决 Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘ (2)问题

GE口:SGMII模式和serdes模式

Redis.conf详解

owasp top10 渗透测试
随机推荐
MySQL gets the self incrementing line mark (different from MySQL version)
Detailed analysis of the process (life cycle) of class loading
【LVGL(3)】设置对象大小、位置、盒子模型、状态
Sealos packages and deploys kubesphere container platform
Camera Hal OEM模块 ---- cmr_grab.c
Experiment: creation, expansion, and deletion of LVM logical volumes
类加载的过程(生命周期)详情分析
[media controller] open source project learning notes (based on Arduino micro development board)
Redis basic type - ordered set Zset
Metaltc5.0 realizes webrtc version IPC of Junzheng pure C
Take you to understand the inventory deduction principle of MySQL database
Redis基本类型-有序集合Zset
分组后返回每组中的最后一条记录 GROUP_CONCAT用法
STM32外部中断(寄存器版本)
Three level classification / menu query tree structure
【学习笔记】网页出现白屏可能的原因与优化方法
Geek planet ByteDance one stop data governance solution and platform architecture
reflex
Identification of Chinese medicinal materials
神经网络超参数调整(基于ray包)