vue如何监听keydown事件在window上

window.onkeydown = function(){
        console.log(123)
    }

在vue中如何写,绑定在哪里?

created:function(){
          var _this = this;
          window.onkeydown = function(e){
            _this.aasd = e.keyCode;
        }
      }

这样写了

阅读 11.8k
3 个回答

可以考虑在根组件的 created 钩子里写

我习惯写到ready里

<template>
<input type="text" class="add-event" placeholder="请输入要做的事情" v-model="msg" @keyup.enter="log">
</template>  

<script>
    export default {
        data() {
            return {
                 msg: '',
                test: ['hello', 'hello2'],
            }
        },
        methods: {
            log: function () {
                this.test.push(this.msg);
            }
        }
    }
</script>  
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题