vue通过快捷键保存 ctrl+s或者command+s

直接上代码

save (e) {
  if ((e.ctrlKey || e.metaKey) && e.key === 's') {
    // 执行save方法
    this.save()
    // 阻止默认事件
    e.preventDefault()
}

在mounted里通过addEventListener监听

document.addEventListener('keydown', this.save, false)

别忘记在离开页面的时候在destroyed里面销毁

document.removeEventListener('keydown', this.save, false)

评论

暂无评论