当前位置:网站首页>Applet custom components - data, methods, and properties
Applet custom components - data, methods, and properties
2022-07-28 06:56:00 【Like Feynman%】
1. In applet components , Private data for component template rendering , Need to define to data node , Examples are as follows :
// stay js file
data: {
count:0
},
2. In applet components , Event handling functions and custom methods need to be defined to methods In the node , The example code is as follows :
// Components js file
methods: {
addCount(){
this.setData({
count:this.data.count+1
})
console.log(this.data.count)
this._showCount()
},
_showCount(){
wx.showToast({
title: 'count The value of is :'+this.data.count,
icon:'none'
})
}
}
// Components wxml file
<button bindtap="addCount" type="warn"> Click on </button>
<view>{
{count}}</view>
3.properties attribute
In applet components ,properties Is the external attribute of the component , It is used to receive the data transferred from the outside to the component , The example code is as follows :( send count The maximum value is 10)
properties: {
max:{// How to fully define attributes , It is recommended to use this way
type:Number,// The data type of the attribute value
value:10// Property defaults
},
//max:Number// Simplify how attributes are defined 【 When there is no need to specify the default value of the attribute 、 Simplified methods can be used 】
},
// The page that references the component
<test1 max="10"></test1>4.data and properties The difference between
In the components of the applet ,properties Properties and data The usage of data is the same , They are all Can read but write Of , It's just :
- data Prefer to store private data of components
- properties It is more inclined to store values passed from the outside to components
showInfo(){
console.log(this.data)//{count: 1, max: 10}
console.log(this.properties)//{count: 1, max: 10}
// The result is true, Prove that the two are essentially the same
console.log(this.data === this.properties)//true
}5. use setData modify properties Value
because data Data and properties Attributes are essentially the same , therefore properties Property can also be used for page rendering , Or use setData by properties Property assignment in
// In the component's js In file methods In the method
addMax(){
this.setData({
max:this.properties.max+1
})
// In the component's wxml
<view>max The attribute value :{
{max}}</view>
<button bindtap="addMax" type="primary"> Click on </button>
Code run results :
边栏推荐
- Technology sharing | sending requests using curl
- Network - transport layer (detailed version)
- [C language] custom structure type
- Test interview questions collection (III) | computer network and database (with answers)
- Gerapy use
- 测试人生 | 二线城市年薪超40W?疫情之下涨薪100% + 是怎么做到的?
- shell脚本——sort、uniq、tr、数组排序、cut、eval命令配置
- ---栈&队列---
- Wechat applet custom compilation mode
- Teach you three steps to complete the construction of the test monitoring system hand in hand
猜你喜欢

What is the good brand of air conduction Bluetooth headset and the best brand recommendation of air conduction headset

iptables防火墙

How to simulate the implementation of strcpy library functions

DNS domain name resolution service

Technology sharing | interface testing value and system

prometheus监控nacos

Which is the best one to make air conduction headphones? Inventory of the best air conduction headphones

On cookies and session

DNS forward resolution experiment

DNS正向解析实验
随机推荐
ISO 3.0-server three power separation configuration
Vmware workstation configuration net mode
技术分享 | 实战详解接口测试请求方式Get、post
Test interview questions collection (I) | common required questions and procedures of software testing (with answers)
Question brushing record - linked list
shell脚本——正则表达式
Create, traverse and search nodes as required for single linked list
Teach you three steps to complete the construction of the test monitoring system hand in hand
技术分享 | 接口测试价值与体系
MySQL master-slave
软件开发中常见模型
Tcp/ip five layer model
yapi漏洞挂马程序chongfu.sh处理
---Stack & queue---
2022 Tanabata gift recommendation! Nice, cheap and practical gift recommendation
Lancher deployment practice
Test life | second tier cities with an annual salary of more than 40W? How did you achieve 100% salary increase under the epidemic?
Suger Bi create task
Qgraphicsview promoted to qchartview
cocos2d-x 学习笔记——瓦片地图TiledMap