当前位置:网站首页>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 .
边栏推荐
- Simplicity Studio无法识别新买的JLink v9解决方法
- C # judge whether the array contains any items of another array
- Automatic 3D Detection and Segmentation of Head and Neck Cancer from MRI Data.
- Data visualization: the four quadrants of data visualization teach you to correctly apply icons
- 官方stm32芯片包下载地址 stm32f10x stm32f40x下载
- After installing anaconda, you need to enter a password to start jupyterlab
- UE4 material UV texture does not stretch with model scale
- 监控数据源连接池使用情况
- Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
- 数据可视化:数据可视化四象限,教你正确应用图标
猜你喜欢

Yotact real-time instance segmentation

Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit

Print service IP setting scheme

遍历vector容器中的对象的方式

The principle of session and cookie

数据可视化:数据可视化四象限,教你正确应用图标

Matlab tips (21) matrix analysis -- partial least squares regression

Do you know what BFD is? This article explains the principle and usage scenarios of BFD protocol in detail

Factory mode

GD32F4xx 以太網芯片(enc28j60)驅動移植
随机推荐
Understanding of singleton mode
Making of simple addition calculator based on pyqt5 and QT Designer
基于stm32标准库独立按键的多按键状态机的实现
Implementation of multi key state machine based on STM32 standard library
数据处理时代,数据质量建设才是企业的生存之道
Segmentation of Head and Neck Tumours Using Modified U-net
Western Polytechnic University, one of the "seven national defense schools", was attacked by overseas networks
Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images
Lc236. nearest common ancestor of binary tree
Data governance: the solution of data governance in the data Arena
LSM6DSL之SPI驱动
LeetCode刷题——泰波那契数列
UE4 material UV texture does not stretch with model scale
证券账号开户安全吗?是靠谱的吗?
Chang'an chain go language smart contract writing and compilation
Chapter 12 signals (II) - examples of producers and consumers
基于PyQt5和Qt Designer的简易加法计算器的制作
【NOI模拟赛】为NOI加点料(重链剖分,线段树)
数据源连接池未关闭的问题 Could not open JDBC Connection for transaction
Pytoch Summary - transform between sensor and numpy