当前位置:网站首页>为定时器和延时器等其它情况的回调函数绑定当前作用域的this
为定时器和延时器等其它情况的回调函数绑定当前作用域的this
2022-07-01 18:46:00 【王元肉】
前置场景
mounted() {
console.log("this",this);
setTimeout(function() {
console.log("init",this);
},1000);
},
想在延时器的回调函数中访问当前Vue实例怎么办?
方式1 额外变量(不推荐)
mounted() {
console.log("this",this);
const that = this;
setTimeout(function() {
console.log("init",that);
},1000);
}
创建了多余的变量,不推荐使用。
方式2 箭头函数
mounted() {
console.log("this",this);
setTimeout(
()=>{
console.log("箭头函数",this);
},
1000
);
},
使用箭头函数,this是父作用域中的this,当时与此同时箭头函数中arguments也是父作用域中arguments。
方式3 使用bind
mounted() {
console.log("this",this);
setTimeout(function () {
console.log("bind",this);
}.bind(this),1000)
},
边栏推荐
- AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
- pickle. Load error [attributeerror: can't get attribute 'volatile' on < module '\u main']
- Audio and video, encoding and decoding related e-books, gadgets, packaged for free!
- 测试自学人必看:软件测试如何找测试项目?
- 2022/6/8-2022/6/12
- Use the uni app demo provided by Huanxin to quickly realize one-on-one chat
- JS 之 常用内置类的使用
- Facebook聊单,SaleSmartly有妙招!
- GB28181的NAT穿透
- 再回顾集合容器
猜你喜欢
GB28181之SIP协议
科技T3国产平台!成功搭载“翼辉国产实时系统SylixOS”
Ubuntu14 install MySQL and configure root account local and remote access
HLS4ML报错The board_part definition was not found for tul.com.tw:pynq-z2:part0:1.0.
A brief understanding of white box encryption technology
Crunch简介、安装,使用Crunch制作密码字典
Instagram 为何从内容共享平台变成营销工具?独立站卖家如何利用该工具?
uni-app商品分类
JVM memory model
Oracle物理体系结构
随机推荐
研究了11种实时聊天软件,我发现都具备这些功能…
Flutter 实战-快速实现音视频通话应用
Compile ffmpeg source code with msys+vs2019 under win10
[info() method in org.slf4j.logger]
Redo和Undo的区别
GB28181之SIP协议
Proxy in ES6
HLS4ML报错The board_part definition was not found for tul.com.tw:pynq-z2:part0:1.0.
Use the uni app demo provided by Huanxin to quickly realize one-on-one chat
Uni app product classification
JDBC中如何添加事务
ffmpeg 错误码
Difference between redo and undo
JVM内存模型
Uni app wechat applet one click login to obtain permission function
Werewolf killing strategy: do you think I'm easy to cheat? Who do we believe!
如何正确使用Vertx操作Redis(3.9.4带源码分析)
optaplanner学习笔记(一)案例Cloud balance
使用 Kibana Timelion 进行时间序列分析
How to solve the problem of splash screen when the main and sub code streams of easygbs are h.265?