当前位置:网站首页>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 .
边栏推荐
- Closed door cultivation (24) shallow understanding of cross domain problems
- 微信小程序实现数据侦听器watch,包含销毁watch和子属性的watch
- After aikuai is installed or reset, the PC or mobile terminal cannot obtain IP
- 基于keil5自动配置stm32f103标准库的官网freertos移植
- CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
- Gd32f4xx Ethernet Chip (ENC28J60) Drive transplantation
- The 23 most useful elasticsearch search techniques you must know
- Iso16000-9 testing of volatile organic compounds in building products and furniture
- 装饰器模式的应用,包装ServletRequest,增加addParameter方法
- 数据处理时代,数据质量建设才是企业的生存之道
猜你喜欢

基於PyQt5和Qt Designer的簡易加法計算器的制作

自定义mvc框架实现

Factory mode

LSM6DSL之SPI驱动

Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning

转载 :判断对象是否具有属性的5种方法

长安链GO语言智能合约编写与编译

长安链数据存储介绍及Mysql存储环境搭建

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

用户级线程和内核级线程
随机推荐
五心公益红红娘团队
Factory mode
UE4 动画重定向
遍历vector容器中的对象的方式
Mh/t 6040 smoke density test of aviation materials
How to set Google Chrome as the default browser
A comparison of methods for fully automatic segmentation of tumors and involved nodes in PET/CT of h
长安链GO语言智能合约编写与编译
Matlab tips (21) matrix analysis -- partial least squares regression
Hystrix熔断器:服务熔断与服务降级
IDEA自动补全
Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
Mysql配置主从数据库
Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)
装饰器模式的应用,包装ServletRequest,增加addParameter方法
你知道BFD是什么吗?一文详解BFD协议原理及使用场景
Print service IP setting scheme
IDEA调试失败,报JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_LOAD(196)
CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION