


Sharing practical examples from getting started with WeChat mini programs (4)
Debugging method
Debugging of small programs is similar to general web debugging, but pay attention to a few points:
Selection of debugging files
Open the debugger, select the Sources
tab, and then use the shortcut key: ctrl+p
to open the search box and select the file with the suffix sm
Perform debugging.
Display of data binding
Open the debugger and select AppData
tab, you can check the detailed information of page data binding and whether it is bound correctly. Records the data binding content of all pages.
Through the above picture, we can know that only the datesArry
object contains data that needs to be bound to the page.
Template
Template
wxml / wxss / js
Module reuse, better encapsulation of UI and business logic to increase reusability
When an area needs to be used in multiple places, you can make this area into a template and call this template when using it. This will reduce the writing of repeated code, is easy to maintain, and makes the code tidy. .
Writing template code
<template name='postItem'> <view class='box'> <view class='title-box'> <text class='title'>{{item.title}}</text> <text class='date'>{{item.date}}</text> </view> <image src='{{item.imgUrl}}' class='cont-image'></image> <text class='content'>{{item.content}}</text> </view> </template>
In order to facilitate the management of our template files, you can place them under the pages
folder, Create a new template file, which stores the written template; we need to use the tag template
to wrap the code, and add a name
attribute to it. Of course, we must also write the corresponding style, and There is no difference in the normal way of writing styles.
The newly created template file can only store the wxml
and wxss
files of the template. Currently, putting other file types in the template file of the mini program does not work. It cannot be reused, although no error is reported, such as js
files. The
template
tag is just a placeholder, telling the compiler that this is where the template code is loaded. When the page is compiled, the tag disappears, so it is necessary to respond to events with the template file. , we need to wrap the template file with a tag that can add events in the area where the template file is loaded, such as: view / block
##
- Use the template code
Generally introduce it at the beginning of the file corresponding to the template file to be imported:
// 在对应的wxml文件中开头引入 <import src = '模版wxml文件路径' /> // 在对应的wxss文件中开头引入,注意末尾的分号 @import '模版wxss文件路径' ;
wxml file that introduces the template file, use
<template is = '模版的名字' data="{{要绑定到模版的数据}}"
// bolck标签的作用是作用事件到template模版上面 <block wx:for=" 服务器获取的数据集" wx:for-item='item' wx:for-index='index' > <template is = '模版的名字' data="{{要绑定到模版的数据,一般是服务器获取的一个数据对象item}}" </block>
wx:for=' ' , you can know the corresponding sub-object data and its index value through the
item / index obtained by default; but we found that
item must be used in front of each template-bound data item. Attribute value (attribute value of sub-object) is used to bind data.
item is redundant. Is there any way to simplify it? Of course!
<view wx:for='{{datesArry}}' wx:for-item='item' wx:for-index='idx'> // 在对应的item的前面加上 ... 三个点 <template is='postItem' data='{{...item}}' /> // <view>{{idx}}</view> </view>
... three dots in front of the loop's sub-object
item. A simple line of code implements the page below.
...)
data-, binds some information we need to store, and passes it to other places, such as the clicked page article index.
- Set custom attributes
view before you can operate it; the reason is that the
template tag is just a placeholder and disappears after compilation.
<view wx:for='{{datesArry}}' wx:for-item='item' data-postId='{{item.postId}} wx:for-index='idx' catchtap='onPostTap' '> <template is='postItem' data='{{...item}}' /> </view>
item.postId is obtained from the server and is an attribute in the data source.
- Get custom attributes
onPostTap:function(event){ var postId=event.currentTarget.dataset.postid; var postIdData = event.currentTarget.dataset; console.log(postId); // 1 console.log(postIdData); }
event .currentTarget.dataset.postid: Obtained custom attribute value
event
Event object
event.currentTarget
:当前点击的目标元素event.currentTarget.dataset
:目标元素上面的自定义属性集合
通过获取到的自定义属性,可以作为一个参数绑定一个链接上面,达到不同栏位的点击跳转到不同的页面:
onPostTap:function(event){ var postId=event.currentTarget.dataset.postid; wx.navigateTo({ url: 'xxxx?id='+postId, }) }
上面只是说明了在同一个页面之间获取自定义属性,要是在不同页面之间怎么获取自定义属性,达到传递值的作用呢?
wx.navigateTo({ url: 'xxxx?id='+postId, })
通过上面的方法把参数postId
,传递出去,然后在要接收的页面中使用options.id
来获取
onLoad: function (options) { var postId = options.id; console.log(options); }
options.id
就是通过wx.navigateTo
传递过去的postId
相关推荐:<br>
<a href="http://www.php.cn/js-tutorial-386466.html" target="_self">微信小程序template模板引用实例分享</a>
The above is the detailed content of Sharing practical examples from getting started with WeChat mini programs (4). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Quark Netdisk and Baidu Netdisk are very convenient storage tools. Many users are asking whether these two softwares are interoperable? How to share Quark Netdisk to Baidu Netdisk? Let this site introduce to users in detail how to save Quark network disk files to Baidu network disk. How to save files from Quark Network Disk to Baidu Network Disk Method 1. If you want to know how to transfer files from Quark Network Disk to Baidu Network Disk, first download the files that need to be saved on Quark Network Disk, and then open the Baidu Network Disk client. , select the folder where the compressed file is to be saved, and double-click to open the folder. 2. After opening the folder, click "Upload" in the upper left corner of the window. 3. Find the compressed file that needs to be uploaded on your computer and click to select it.

1. First, we enter NetEase Cloud Music, and then click on the software homepage interface to enter the song playback interface. 2. Then in the song playback interface, find the sharing function button in the upper right corner, as shown in the red box in the figure below, click to select the sharing channel; in the sharing channel, click the "Share to" option at the bottom, and then select the first "WeChat Moments" allows you to share content to WeChat Moments.

Recently, Baidu Netdisk Android client has ushered in a new version 8.0.0. This version not only brings many changes, but also adds many practical functions. Among them, the most eye-catching is the enhancement of the folder sharing function. Now, users can easily invite friends to join and share important files in work and life, achieving more convenient collaboration and sharing. So how do you share the files you need to share with your friends? Below, the editor of this site will give you a detailed introduction. I hope it can help you! 1) Open Baidu Cloud APP, first click to select the relevant folder on the homepage, and then click the [...] icon in the upper right corner of the interface; (as shown below) 2) Then click [+] in the "Shared Members" column 】, and finally check all

Mango TV has various types of movies, TV series, variety shows and other resources, and users can freely choose to watch them. Mango TV members can not only watch all VIP dramas, but also set the highest definition picture quality to help users watch dramas happily. Below, the editor will bring you some free Mango TV membership accounts for users to use, hurry up and take a look Take a look. Mango TV latest member account free sharing 2023: Note: These are the latest member accounts collected, you can log in directly and use them, do not change the password at will. Account number: 13842025699 Password: qds373 Account number: 15804882888 Password: evr6982 Account number: 13330925667 Password: jgqae Account number: 1703

Implementing card flipping effects in WeChat mini programs In WeChat mini programs, implementing card flipping effects is a common animation effect that can improve user experience and the attractiveness of interface interactions. The following will introduce in detail how to implement the special effect of card flipping in the WeChat applet and provide relevant code examples. First, you need to define two card elements in the page layout file of the mini program, one for displaying the front content and one for displaying the back content. The specific sample code is as follows: <!--index.wxml-->&l

According to news from this site on October 31, on May 27 this year, Ant Group announced the launch of the "Chinese Character Picking Project", and recently ushered in new progress: Alipay launched the "Chinese Character Picking-Uncommon Characters" mini program to collect collections from the society Rare characters supplement the rare character library and provide different input experiences for rare characters to help improve the rare character input method in Alipay. Currently, users can enter the "Uncommon Characters" applet by searching for keywords such as "Chinese character pick-up" and "rare characters". In the mini program, users can submit pictures of rare characters that have not been recognized and entered by the system. After confirmation, Alipay engineers will make additional entries into the font library. This website noticed that users can also experience the latest word-splitting input method in the mini program. This input method is designed for rare words with unclear pronunciation. User dismantling

People in the workplace will be familiar with PPT production, because whether it is a year-end summary or a work report, many companies require it to be presented in the form of PPT. At this time, I encountered a problem, that is, how to share PPT? Don’t worry, the editor below will show you how to share PPT. 1. First select the edited PPT and click Save in the upper left corner (if you are using WPS, you can click Login first). 2. Then click the share icon in the menu bar as shown below. 3. Then the sharing interface as shown below will pop up. You can see that a sharing link will appear. Click to send the link to share. 4. You can also click "Allow friends to edit" in the lower left corner of the picture below, so that friends can also click to edit this PPT. 5. If necessary, give P

How uniapp can achieve rapid conversion between mini programs and H5 requires specific code examples. In recent years, with the development of the mobile Internet and the popularity of smartphones, mini programs and H5 have become indispensable application forms. As a cross-platform development framework, uniapp can quickly realize the conversion between small programs and H5 based on a set of codes, greatly improving development efficiency. This article will introduce how uniapp can achieve rapid conversion between mini programs and H5, and give specific code examples. 1. Introduction to uniapp unia
