Vue 按enter键实现登陆 过程

首先在mounted中绑定监听事件

mounted(){
    //绑定事件
     window.addEventListener('keydown',this.keyDown);
  }

在写一个监听事件的方法

 methods:{
    keyDown(e){
        //如果是回车则执行登录方法
      if(e.keyCode == 13){
        this.login();
      }
    }
}

最后要销毁事件

	destroyed(){
        window.removeEventListener('keydown',this.keyDown,false);
  }

猜你喜欢

转载自blog.csdn.net/weixin_40648700/article/details/112170805