当前位置:网站首页>Wechat applet rewrites the page function to realize global logging
Wechat applet rewrites the page function to realize global logging
2022-06-29 09:51:00 【milugloomy】
summary
In the last article 《 The implementation of wechat applet is similar to Vue Global routing for , Implement logging 》 in , By rewriting wx.navigateTo,wx.switchTab,wx.navigateBack These three methods implement functions similar to global routing , However, there are still some limitations after rewriting these three methods , Unable to intercept bottom tab Click event of and return event of physical return key of Android phone .
If you don't need to intercept before jumping , So there is a better solution , rewrite Page function .
Solution
newly build myPage.js file , The code is as follows :
let data
// here data from app.js In the middle of , In order to obtain data Some parameters of
function init(_data) {
data = _data
// rewrite page function , Add Alibaba cloud monitoring and logging
let oldPage = Page
Page = function(obj) {
// rewrite onShow Method , Save the old with a variable onShow function
let oldOnShow = obj.onShow
obj.onShow = function() {
enterPageLog()
// It can't be written here oldOnShow(), Otherwise no this,this.setData And so on undefined. there this stay Page Only when the constructor is instantiated will it be specified
// stay Page When the constructor is instantiated , The applet will change the current Page Prototype chain of objects (__proto__) Add many methods , for example setData. Current obj No, setData
// The above paragraph is my guess
oldOnShow.call(this)
}
// rewrite onHide Method , Save the old with a variable onHide function
let oldOnHide = obj.onHide
obj.onHide = function() {
leavePageLog()
// It can't be written here oldOnHide(), Otherwise no this,this.setData And so on undefined. there this stay Page Only when the object is instantiated will it be specified
oldOnHide.call(this)
}
return oldPage(Monitor.hookPage(obj))
}
}Enclose this string of code in a js In file , And then in app.js Introduction in , stay app.js Of onLaunch Method init Method .
import myPage from '/utils/myPage'
App({
data: {},
onLaunch: function() {
// rewrite page function
myPage.init(this.data)
},
...
})In the above code ,
enterPageLog() and leavePageLog() To keep a log , In this way, public logging is realized , There is no need to write duplicate logging code on each page .
Monitor.hookPage(obj) It is Alibaba cloud's front-end monitoring , The official plan is in every Page The parameter transfer object of the function contains a layer , as follows :
Page(Monitor.hookPage(options));rewrite Page after , It is not necessary to be in every Page Add this string to all objects .
Key code interpretation
let oldOnShow = obj.onShow
obj.onShow = function() {
enterPageLog()
oldOnShow.call(this)
}rewrite onShow function , Finish the customization , Call old onShow Function time , Can not write oldOnShow(). If written as oldOnShow(), An error will be reported at run time ,this.setData、this.createSelectorQuery And so on undefined.
The reason is that app Of onLaunch In the method , Pass in Page The object of the function has not yet setData Other methods , That's the code above onShow Medium this, That is to say obj. stay Page When the constructor is instantiated , The applet will change the current Page The prototype chain of functions (prototype) Add many methods , for example setData. You can view this one 《 From the source code, wechat applet startup process 》 Understand the process of applet starting and loading each object .
Use here oldOnShow.call(this) To delay the assignment this, This this Only in Page It is only specified when instantiating , That is to say Page Many methods are added after instantiation this. This avoids the present Page No, setData、createSelectorQuery And so on .call The principle and usage of can be found in this article 《 In depth understanding of JavaScript Medium Call、Apply、This》.
Conclusion
This is just a simple logging function , By rewriting Page, Other public functions can also be realized , Including adding public functions 、 Parameters 、 event , Authority judgment, etc .
JS Language features can override many native or systematic methods , Reasonable rewriting can achieve many common functions , Reduce development .
边栏推荐
- How to set Google Chrome as the default browser
- Pytorch learning summary - memory cost of operation
- Reading notes on how to connect the network - Web server request and response (IV)
- # 《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(四)
- Data visualization: the four quadrants of data visualization teach you to correctly apply icons
- es报错NoNodeAvailableException[None of the configured nodes are available:[.127.0.0.1}{127.0.0.1:9300]
- 監控數據源連接池使用情况
- UE4 compile a single file (VS and editor start respectively)
- In the era of data processing, data quality construction is the way for enterprises to survive
- Reading notes on how to connect the network - Web server request and response (V)
猜你喜欢

UE4 编译单个文件(VS与编辑器分别启动)

UE4 animation redirection

Closed training (25) basic web security

KiCad学习笔记--快捷键

通用分页框架

【华为认证】HCIA-DATACOM史上最全精选题库(附答案解析)

In the era of data processing, data quality construction is the way for enterprises to survive

用户级线程和内核级线程

Chang'an chain go language smart contract writing and compilation

Simplicity Studio无法识别新买的JLink v9解决方法
随机推荐
自定义mvc框架实现
数据处理时代,数据质量建设才是企业的生存之道
Mh/t 6040 smoke density test of aviation materials
Simplicity Studio无法识别新买的JLink v9解决方法
数据治理:数据标准管理(第三篇)
zabbix4.4配置监控服务器指标,以及图形页乱码解决
[noi Simulation Competition] add points for noi (heavy chain dissection, line segment tree)
长安链GO语言智能合约环境搭建及使用
Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
LC236. 二叉树的最近公共祖先
programing language
Cloud management platform: openstack architecture design and detailed interpretation
基于PyQt5和Qt Designer的简易加法计算器的制作
Hb5470 combustion test of non-metallic materials in civil aircraft cabin
Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images
证券账号开户安全吗?是靠谱的吗?
Construction and use of Changan chain go language smart contract environment
In the future of Business Intelligence BI, how do you view the ai+bi model?
你知道BFD是什么吗?一文详解BFD协议原理及使用场景
A 2.5D Cancer Segmentation for MRI Images Based on U-Net