为什么在 Vue.js 上 @keydown.alt 有效,而 @keyup.alt 无效?

Why on Vue.js @keydown.alt works, but @keyup.alt doesn't?

我有一个变量,我想在按下 alt 键时设置为 1,在没有按下时设置为 0,但结果是 Vue 仅执行 @keydown.alt.

我试过使用 enter 键,它工作得很好,在两种状态之间切换,但我不明白为什么它不能使用 alt 键。

    @keydown.alt="scheda++"
    @keyup.alt="scheda--"

来自 Vuejs documentation:

Note that modifier keys are different from regular keys and when used with keyup events, they have to be pressed when the event is emitted. In other words, keyup.ctrl will only trigger if you release a key while holding down ctrl. It won’t trigger if you release the ctrl key alone. If you do want such behaviour, use the keyCode for ctrl instead: keyup.17

但是对于 Alt,如果您使用 Windows,则还有一个问题。 在 Windows 系统上,window 将按下 Alt 解释为想要打开 window 的菜单,而 keyup 则不是触发了。我们想要防止默认行为,我们可以使用 event.preventDefault() 来做到这一点,或者在 Vue 世界中,.prevent event modifier.

new Vue({
  el: '#app',
  data: {
    counter: 0
  },
  methods: {
   increment: function() {
     console.log('inc')
     this.counter += 1;
    },
    decrement: function() {
     console.log('dec')
     this.counter -= 1;
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <input @keydown.18.prevent="increment()" @keyup.18.prevent="decrement()">
  <p >{{ counter }}</p>
</div>