Vue+element button button bind enter key event

< template >

<div>
    <userDetail v-if="userdetailShow"></userDetail>
    <el-button @click="detailShow"></el-button>
</div>

< / template >

data () {

return{
    userdetailShow:false
}

}

created () {

this.keyupSubmit()

},
methods: {
/ / listen for carriage return events

keyupSubmit(){
  document.onkeydown=e=>{
    let _key=window.event.keyCode;
    if(_key===13){
      this.adduser()
    }
  }
},
detailShow(){
    this.userdetailShow=true
}

}

there is a definite button button in the userdetai component, which listens to the carriage return event code in the userdetail component, and globally binds the method adduser () {/ p > in the carriage return event component.

this.userdetailShow=false
}

the enter event binding succeeded, but the display property of userdetail, the dailog component v-if binding, changes from true to false to true after the enter event.

according to the requirements and the logic of the code, the functions bound by the carriage return event are all run, but I don"t know why the thsi.userdetailShow=false has become true

.

but if the dailog component pops up, if you click dailog and give the dailog focus, this situation will not happen

ask what the reason is.

Mar.23,2021

try changing v-if to v-show!
v-if is a 'real' conditional rendering because it ensures that event listeners and subcomponents within the conditional block are properly destroyed and rebuilt during switching.

v-if is also lazy, and if the condition is false at the initial rendering, nothing is done-- the conditional block does not start to render until the condition is true for the first time, compared to
v-show

.

is much simpler-no matter what the initial condition is, the element is always rendered and simply switched based on css.

generally speaking, v-if has higher handover overhead,
and v-show has higher accident rendering overhead. Therefore, if you need to switch very frequently, it is better to use v-show; if

Runtime conditions are unlikely to change, so it's better to use v-if.

Menu