简体   繁体   English

Vue.js 强制在函数内部重新加载

[英]Vue.js force reload INSIDE the function

Small piece of code, when on mouseover I want an input field is visible and focus on it:一小段代码,当鼠标悬停时,我希望输入字段可见并专注于它:
Template:模板:

<span @mouseover="hoverOn('givenName')">
    <input v-show="this.hovers['givenName']" ref="givenName" v-model="...

and

methods: { 
    hoverOn: function (name) { 
      this.hovers[name] = true 
      this.$refs[name].focus() 
    }, ... 

The field becomes visible but the focus is not done.该字段变为可见,但焦点未完成。 If I mouseover a 2nd time, as the field is now visible, the focus works.如果我第二次将鼠标悬停在该字段上,则该字段现在可见,因此焦点有效。 So parameters of the function are OK, the hovers array is correctly declared in the data section.所以函数的参数没问题,在数据部分正确声明了悬停数组。
This is probably because Vue has not yet set the field visible when the focus is called.这可能是因为 Vue 尚未设置调用焦点时可见的字段。 I tried to insert a this.$forceUpdate() between the 1st line and the focus lines but this does not work.我试图在第一行和焦点线之间插入一个 this.$forceUpdate() 但这不起作用。

You are correct - the reason it doesn't focus is because the element hasn't rendered yet.你是对的 - 它没有聚焦的原因是因为元素还没有渲染。 Put the focus call in a nextTick callback and vue will wait until the element has rendered to call it.将焦点调用放在nextTick 回调中,vue 将等到元素渲染后调用它。

methods: {
  hoverOn: function (name) {
    this.hovers[name] = true;
    this.$nextTick(() => {
        this.$refs[ref].focus()
    })
},

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

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