js & 快捷键 & vue bind bug
js & 快捷键 & vue bind bug
how to prevent addEventListener bind many times
solution
dataset & once flag
// flag
// shortcut keys
keyboardShortcutKeys() {
let that = this;
let body = document.querySelector(`body`);
let bindFlag = body.dataset.bindFlag;
// console.log(`bindFlag =`, bindFlag);
if (bindFlag === "true") {
console.log(`only need binding once!`);
} else {
body.dataset.bindFlag = "true";
// bind once
body.addEventListener("keyup", function(e) {
let key = e.which || e.keyCode;
if(e.which === 17) {
// init
this.isCtrlPressed = false;
}
});
body.addEventListener("keydown", function(e) {
let key = e.which || e.keyCode;
if(e.which === 17) {
this.isCtrlPressed = true;
}
let isCtrl = this.isCtrlPressed;
if(e.which === 83 && isCtrl) {
console.log(`you pressed Ctrl + S`);
if (that.onClickButton) {
that.onClickButton(`save`);
} else {
console.log(`typeof(that.onClickButton) =`, typeof(that.onClickButton));
console.log(`typeof(that.xyz) =`, typeof(that.xyz));
}
}
if(e.which === 68 && isCtrl) {
console.log(`you pressed Ctrl + D`);
if (that.clickGetNewsInfos) {
that.clickGetNewsInfos(`prev`);
}
}
if(e.which === 70 && isCtrl) {
console.log(`you pressed Ctrl + F`);
if (that.clickGetNewsInfos) {
that.clickGetNewsInfos(`next`);
}
}
if(e.which === 88 && isCtrl) {
console.log(`you pressed Ctrl + X`);
if (that.clickShowResureModal) {
that.clickShowResureModal(`passed`);
}
}
});
}
},
// shortcut keys
keyboardShortcutKeys() {
let that = this;
let body = document.querySelector(`body`);
let bindFlag = body.dataset.bindFlag;
// console.log(`bindFlag =`, bindFlag);
if (bindFlag === "true") {
console.log(`only need binding once!`);
} else {
body.dataset.bindFlag = "true";
// bind once
body.addEventListener("keyup", function(e) {
let key = e.which || e.keyCode;
if(e.which === 17) {
// init
this.isCtrlPressed = false;
}
});
body.addEventListener("keydown", function(e) {
let key = e.which || e.keyCode;
if(e.which === 17) {
this.isCtrlPressed = true;
}
let isCtrl = this.isCtrlPressed;
if(e.which === 83 && isCtrl) {
console.log(`you pressed Ctrl + S`);
if (that.onClickButton) {
that.onClickButton(`save`);
}
}
if(e.which === 68 && isCtrl) {
console.log(`you pressed Ctrl + D`);
if (that.clickGetNewsInfos) {
if (!that.isFirstNews) {
that.clickGetNewsInfos(`prev`);
} else {
// btn bug
console.log(`prev btn bug!`);
}
}
}
if(e.which === 70 && isCtrl) {
console.log(`you pressed Ctrl + F`);
if (that.clickGetNewsInfos) {
if (!that.isLastNews) {
that.clickGetNewsInfos(`next`);
} else {
// btn bug
console.log(`next btn bug!`);
}
}
}
if(e.which === 88 && isCtrl) {
console.log(`you pressed Ctrl + X`);
if (that.clickShowResureModal) {
that.clickShowResureModal(`passed`);
}
}
});
}
},
bug
快捷键,与浏览器自带的冲突了
- 保存CTRL + Alt + S;上一条CTRL + Alt + D;下一条CTRL + Alt + F;处理通过CTRL + Alt + X , windows 可以正常使用

- 保存CTRL+S;上一条CTRL+D;下一条CTRL+F;处理通过CTRL+X, MacOS 可以正常使用

https://codepen.io/webgeeker/full/YBPBOV
js & 快捷键 & vue bind bug的更多相关文章
- vue & lifecycle methods & this bug & ES6 Arrow function & this bind bug
vue & lifecycle methods & this bug ES6 Arrow function & this bind bug bad fetchTableData ...
- iview render bug & vue namespace bug
iview render bug https://codepen.io/xgqfrms/pen/gyGjKP https://codepen.io/xgqfrms/full/gyGjKP bug &l ...
- vue & less bug
vue & less bug bezierEasingMixin(); ^ Inline JavaScript is not enabled. Is it set in your option ...
- vue-cli & plugin:vue/strongly-recommended bug
vue-cli & plugin:vue/strongly-recommended bug ESLint plugin:vue/strongly-recommended module.expo ...
- Vue.js起手式+Vue小作品实战
本文是小羊根据Vue.js文档进行解读的第一篇文章,主要内容涵盖Vue.js的基础部分的知识的,文章顺序基本按照官方文档的顺序,每个知识点现附上代码,然后根据代码给予个人的一些理解,最后还放上在线编辑 ...
- vue.runtime.esm.js:593 [Vue warn]: Invalid prop: custom validator check failed for prop "value".报错解决
在uni中使用 picker组件,一直报错 vue.runtime.esm.js:593 [Vue warn]: Invalid prop: custom validator check failed ...
- js & 快捷键
js & 快捷键 demo js-keyboard-shortcuts.html https://codepen.io/webgeeker/pen/MLYrNq let isCtrl = fa ...
- js原生函数bind
/*在javascript中,函数总是在一个特殊的上下文执行(称为执行上下文),如果你将一个对象的函数赋值给另外一个变量的话,这个函数的执行上下文就变为这个变量的上下文了.下面的一个例子能很好的说明这 ...
- 如何在其他js 引入main.js 中 vue 的实例?
1.原因解析 经测试发现,代码先执行了 index.js >> main.js >> Home.vue scr/api/index.js src/main.js src/co ...
随机推荐
- Redis 为什么使用单进程单线程方式也这么快
Redis 采用的是基于内存的采用的是单进程单线程模型的 KV 数据库,由 C 语言编写.官方提供的数据是可以达到100000+的 qps.这个数据不比采用单进程多线程的同样基于内存的 KV 数据库 ...
- IVF link error错误不显示的问题
遇到一个奇怪的问题,IVF编译链接时显示error LINK: 后面没有具体的错误信息(见后图,我的窗口后面是空的)环境:windows 10 64位系统,VS2017 commutity 版本, ...
- 关于Netty的学习前总结
摘要 前段时间一直在学习netty因为工作忙的原因没有写一个学习的总结,今天抽个空先把总结写了吧.事先声明,本文不会详细的介绍每一个部分不过每个部分都会附上讲解详细的url.本文只是为了解释通Nett ...
- SSH结合EasyUI系统(一)———简单介绍
鉴于前文<不仅仅是吐槽>,决定将自己学过的和在学的东西整理一下放进园子:做一个好园友! 接下来将会持续更新的是近一段时间在学的java web中比较流行的框架SSH(Struts+Spri ...
- react-native初体验(1) — hello world
没有简介,直接开始干活吧. 默认阅读本文的你已经安装好 nodejs, windows用户需要升级yarn到最新版本. 并且设置安装源为国内的淘宝源: npm config set registry ...
- webpack3升级为webpack4
写在前面的话:为什么要升级,因为公司目前的项目使用webpack3,但是因为是多页应用,入口估计有一百多个,开发模式下慢得不像话,修改一个文件需要十几秒才编译好,之前的解决方案是减少入口,但是要调试其 ...
- static笔记
目录 1. static概括 2. static特点 1. 被static修饰的成员变量属于类,不属于这个类的某个对象. 2.被static修饰的成员可以并且建议通过类名直接访问 3. static注 ...
- java四种访问权限修饰符
java中四个访问权限修饰符: public(公开的).protected(受保护的).default(默认的).private(私有的). 它们决定了紧跟其后被定义的东西的使用范围. 适用范围< ...
- 服务治理-> Spring Cloud Eureka
服务治理->搭建服务注册中心 服务治理可以说是微服务架构中最为核心和基础的模块, 它主要用来实现各个微服务 实例的自动化注册与发现. 为什么我们在微服务架构中那么需要服务治理模块呢?微服务 系统 ...
- GitHub笔记(二)——远程仓库的操作
二 远程仓库 1 创建联系 第1步:创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一 ...