当前位置:网站首页>WeChat applet page syntax
WeChat applet page syntax
2022-08-01 00:20:00 【A bunny who loves carrots】
Applet page syntax
一.使用数据
Used by the applet page动态数据are on the corresponding pagejs文件的data内.
// pages/index/index.js
Page({
/** * 1.页面的初始数据 */
data: {
msg: 'Data initialization test'
},
/** * 生命周期函数--监听页面加载 * 加载indexThis function is triggered when the page is displayed,加载 完成后调用onShow()The function renders the page */
onLoad(options) {
console.log('页面加载函数 onLoad()')
// 2.this当前页面的实例对象
console.log(this)
// 3.Use page data
console.log(this.data.msg)
// Edit in 2 seconds
setTimeout(() => {
// 4.修改页面数据
this.setData({
msg: '修改后的msg数据'
})
console.log(this.data.msg)
},2000)
},
/** * 生命周期函数--监听页面初次渲染完成 */
onReady() {
},
/** * 生命周期函数--监听页面显示 */
onShow() {
console.log("onShow()")
},
})

- 单向数据流;model → view
- Modifying data is synchronous;
二. Data hijacking and proxies
1.Object.definePropertyImplement data hijacking and proxying
let person = {
username: '陈三', age: 18
}
let _this = {
}
// 用_this代理person(通过数据劫持set,修改数据时,setterHijack the data to be modified,setter来进行修改;数据代理get)
for (let item in person) {
// console.log(item)
Object.defineProperty(_this, item, {
get() {
return person[item]
},
set(value) {
person[item] = value
}
})
}
console.log(_this)
console.log(_this.username, _this.age)
/* 总结: _this的get()方法获取person的值;数据代理. _this的set()方法修改person的值;数据劫持. */
_this.username = '李四'
_this.age = 81
console.log(_this.username, _this.age)

2.vueThe principle of data proxy and hijacking

三.小程序事件
事件的回调函数with the function period sumdataObject siblings are written directly inPage({})内
1.事件分类
事件 事件分类
冒泡事件 bind+事件 (The event of the target element is fired first,The parent element event is then fired from the inside out)
非冒泡事件 catch+事件 (Only fire the event of the target element) 
四.路由跳转
API → 路由
1.wx.navigateTo
保留当前页面,跳转到应用内的某个页面.最多十层.
url要求: 从/目录开始写/ + pages/页面
/** * Jump routing function,跳转到logs页面 */
//toLogs(){等价于
toLogs:function(){
wx.navigateTo({
url: '/pages/logs/logs',
})
},
2.Configure the specified page window
在对应页面的json内配置
操作:
Write the corresponding content directly, 不用window
{
"navigationBarTitleText": "启动日志", //对应内容
"usingComponents": {
}
}
五.生命周期

The page is displayed for the first time: onLoad → onShow → onReady
// pages/index/index.js
Page({
/** * 1.页面的初始数据 */
data: {
userName: '孤',
msg: 'Data initialization test'
},
/** * Jump routing function,跳转到logs页面 */
toLogs: function () {
wx.navigateTo({
url: '/pages/logs/logs',
})
// wx.reLaunch({
// url: '/pages/logs/logs',
// })
},
/** * 生命周期函数--监听页面加载 * 加载indexThis function is triggered when the page is displayed,加载 完成后调用onShow()The function renders the page */
onLoad(options) {
console.log('页面加载函数 onLoad()')
// debugger;
// 2.this当前页面的实例对象
// console.log(this)
// 3.Use page data
// console.log(this.data.msg)
// Edit in 2 seconds
// setTimeout(() => {
// 4.修改页面数据
// this.setData({
// msg: '修改后的msg数据'
// })
// console.log(this.data.msg)
// },2000)
},
/** * 生命周期函数--监听页面初次渲染完成 */
onReady() {
console.log('初次渲染完成 onReady()')
},
/** * 生命周期函数--监听页面显示 */
onShow() {
console.log("页面显示(多次执行) onShow()")
},
/** * 生命周期函数--监听页面隐藏 */
onHide() {
console.log("页面隐藏 onHide()")
},
/** * 生命周期函数--监听页面卸载 */
onUnload() {
console.log("页面卸载 onUnload()")
},
/** * 页面相关事件处理函数--监听用户下拉动作 */
onPullDownRefresh() {
console.log("用户下拉 onPullDownRefresh()")
},
/** * 页面上拉触底事件的处理函数 */
onReachBottom() {
console.log("上拉触底事件的处理函数 onReachBottom()")
},
/** * 用户点击右上角分享 */
onShareAppMessage() {
console.log("点击右上角分享 onShareAppMessage()")
}
})
边栏推荐
- Binary tree traversal non-recursive program -- using stack to simulate system stack
- 助力数字政府建设,中科三方构建域名安全保障体系
- LeetCode--The problem of robbery
- 类和对象:中
- 微信小程序之小程序页面语法
- [Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
- SQL injection Less42 (POST type stack injection)
- 类和对象:上
- TFC CTF 2022 WEB Diamand WriteUp
- thymeleaf迭代map集合
猜你喜欢
随机推荐
浏览器下载快捷方式到桌面(PWA)
Automated machine learning pycaret: PyCaret Basic Auto Classification LightGBM
微信小程序之小程序页面语法
When can I use PushGateway
游戏安全03:缓冲区溢出攻击简单解释
An open source and easy-to-use flowchart drawing tool drawio
Matlab / ArcGIS 处理GPM全球月均降水数据
Interview Question: Implementing Deadlocks
SQL injection Less47 (error injection) and Less49 (time blind injection)
Flutter教程之 01配置环境并运行demo程序 (教程含源码)
【1161. 最大层内元素和】
命名实体识别-模型:BERT-MRC
类和对象:上
SQL injection Less54 (limited number of SQL injection + union injection)
Web API Introduction and Types
MLP神经网络,GRNN神经网络,SVM神经网络以及深度学习神经网络对比识别人体健康非健康数据
NgRx 里 first 和 take(1) 操作符的区别
Classes and Objects: Above
Rainbow share | how to use moving targets defense technology to guard against the unknown
字符编码和浮点型计算精度丢失问题







![[微服务]分布式事务解决方案-Seata](/img/a8/fc6c24e4d42dfb635bad786cc02164.png)
