当前位置:网站首页>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 | how to do Assertion Verification in interface automated testing?
- MySQL主主
- 单元测试框架Jest搭配TypeScript的安装与配置
- Ubuntu18.04+Centos7配置redis主从【学习笔记】
- Applets: lifecycle
- 如何描述一个BUG以及BUG级别的定义、生命周期
- HDU-1159-CommonSubsequence(LCS最长公共子序列)
- How to simulate the implementation of strcpy library functions
- Dynamic memory management function of C language
- Which is the best one to make air conduction headphones? Inventory of the best air conduction headphones
猜你喜欢
随机推荐
Build php7 private warehouse
技术分享 | 服务端接口自动化测试, Requests 库的这些功能你了解吗?
Tcp/ip five layer model
Hdu-1159-commonsubsequence (LCS longest common subsequence)
File operation in C language
测试面试题集锦(五)| 自动化测试与性能测试篇(附答案)
Lancher deployment practice
Elastic common high frequency commands
shell脚本——sort、uniq、tr、数组排序、cut、eval命令配置
技术分享 | 接口自动化测试中,如何做断言验证?
Teach you three steps to complete the construction of the test monitoring system hand in hand
Yapi vulnerability hanging horse program chongfu.sh processing
Hdu-1097-a hard puzzle (fast power)
QT使用MSVC编译器输出中文乱码问题
进程和线程的区别
VMware Workstation 配置net模式
Regular execution of scratch
Which is the best one to make air conduction headphones? Inventory of the best air conduction headphones
Iptables firewall
HDU-5783 Divide the Sequence(贪心水题)









