简体   繁体   English

有没有办法在没有插件的情况下将快捷方式绑定到 Vue.js 中的按钮?

[英]Is there any way to bind shortcuts to buttons in Vue.js without plugins?

So my question is about binding some keyboard shortcuts, for example v-on:keyup.enter="someEvent()" to buttons in the UI <button :click="someEvent()">$t{{ nameOfBtn }}<button> .所以我的问题是关于绑定一些键盘快捷键,例如v-on:keyup.enter="someEvent()"到 UI 中的<button :click="someEvent()">$t{{ nameOfBtn }}<button> Perhaps there is a way to combine these two in one tag.也许有一种方法可以将这两者结合在一个标签中。 I've been playing around with these two bindings but have not got any desirable result.我一直在玩这两个绑定,但没有得到任何理想的结果。

NB: I cannot use any plugins.注意:我不能使用任何插件。

https://v2.vuejs.org/v2/guide/events.html#Key-Modifiers https://v2.vuejs.org/v2/guide/events.html#Key-Modifiers

You can bind keyup handlers, but it might not make sense in a button.您可以绑定 keyup 处理程序,但在按钮中可能没有意义。 Presses in an input field are captured to the element but most others are global to the page.输入字段中的按下被捕获到元素,但大多数其他按钮是页面的全局。 You could add a listener when your component is created though...您可以在创建组件时添加一个侦听器...

{
  created: function () {
    window.addEventListener('keyup', this.previous)
  },
  methods: {
    previous: function (e) {
      // check key code
    }
  },
  beforeDestroy: function () {
    // remove listener
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM