键盘删除元素

211 阅读1分钟

需求: 选中某一元素,点击键盘Deletej建,删除该元素

1、监听键盘keyup事件

mounted() {
  document.addEventListener('keyup', this.clickkeyup)
},

2、clickkeyup方法

methods: {
    clickkeyup(e) {
      if (this.activeID && e.keyCode === 46) {
        const filterChartInfo = this.chartInfo.filter(item => item.chartId !== this.activeID)
        this.setReport({ reportInfo: filterChartInfo, activeID: '', reportID: this.reportId })
      }
    }
}

说明:chartInfo是如图展示的数组,根据Array.filter()筛选出不是选中的数据。setReport方法是存进store,并实现实时更新。