简体   繁体   English

无法让 Vue 专注于输入

[英]Cant get Vue to focus on input

I'm trying to use this.$refs.cInput.focus() ( cInput is a ref) and it's not working.我正在尝试使用this.$refs.cInput.focus()cInput是一个参考),但它不起作用。 I'd be able to hit g and the input should pop up and the cursor should focus in it, ready to input some data.我可以按g并且输入应该会弹出并且光标应该集中在其中,准备输入一些数据。 It's showing but the focus part is not working.它正在显示,但焦点部分不起作用。 I get no errors in the console.我在控制台中没有错误。

Vue.component('coordform', {
  template: `<form id="popup-box" @submit.prevent="process" v-show="visible"><input type="text" ref="cInput" v-model="coords" placeholder =""></input></form>`,
  data() {
    {
      return { coords: '', visible: false }
    }
  },
  created() {
    window.addEventListener('keydown', this.toggle)
  },
  mounted() {
  },
  updated() {
  },
  destroyed() {
    window.removeEventListener('keydown', this.toggle)
  },
  methods: {
    toggle(e) {
      if (e.key == 'g') {
        this.visible = !this.visible;
        this.$refs.cInput.focus() //<--------not working
      }
    },
    process() {
        ...
    }
  }
});

You can use the nextTick() callback:您可以使用nextTick()回调:

When you set vm.someData = 'new value' , the component will not re-render immediately.当您设置vm.someData = 'new value'时,组件不会立即重新渲染。 It will update in the next “tick”, when the queue is flushed.当队列被刷新时,它将在下一个“滴答”中更新。 [...] [...]

In order to wait until Vue.js has finished updating the DOM after a data change, you can use Vue.nextTick(callback) immediately after the data is changed.为了在数据更改后等到 Vue.js 完成更新 DOM,您可以在数据更改后立即使用Vue.nextTick(callback) The callback will be called after the DOM has been updated. DOM 更新后将调用回调。

( source ) 来源

Use it in your toggle function like:在您的切换功能中使用它,例如:

methods: {
  toggle(e) {
    if (e.key == 'g') {
      this.visible = !this.visible;
      this.$nextTick(() => this.$refs.cInput.focus())
    }
  }
}

Not sure if this is only applicable on Vue3 but if you want to show focus on an input box from inside a component, it's best for you to setup a transition.不确定这是否仅适用于 Vue3,但如果您想从组件内部显示对输入框的关注,最好设置一个转换。 In the transition, there is an event called @after-enter.在过渡中,有一个名为@after-enter 的事件。 So after the animation transition on enter is completed, the @after-enter is executed.所以在enter上的动画过渡完成后,@after-enter就被执行了。 The @after-enter will be the event that will always get executed after your component shows up. @after-enter 将是在您的组件出现后始终执行的事件。

I hope this helps everyone who are having issues with the:我希望这可以帮助所有遇到以下问题的人:

  ***this.$nextTick(() => this.$refs.searchinput.focus());***

It may not be working the way you wanted it to because chances are the initial focus is still on the parent and the input element has not been displayed yet or if you placed it under mounted, it only gets executed once and no matter how you show/hide the component (via v-if or v-show), the nextTick line was already executed on mount() and doesn't get triggered on show of the component again.它可能无法按您希望的方式工作,因为最初的焦点可能仍然在父元素上并且输入元素尚未显示,或者如果您将其放置在已安装的状态下,它只会执行一次,无论您如何显示/hide 组件(通过 v-if 或 v-show), nextTick 行已经在 mount() 上执行,并且不会在组件再次显示时触发。

Try this solution below:请尝试以下解决方案:

<template>
   <transition name="bounce" @after-enter="afterEnter" >
     <div>
         <input type="text" ref="searchinput" />
     </div>
   </transition>
</template>

<script>
methods: {
       afterEnter() {
            setTimeout(() => {
                
                this.$refs.searchinput.focus();
                
            }, 200);
        },
}
</script>

In my case nextTick does not worked well.就我而言,nextTick 效果不佳。

I just used setTimeout like example below:我刚刚使用了 setTimeout,如下例所示:

doSearch () {
  this.$nextTick(() => {
    if (this.$refs['search-input']) {
      setTimeout(() => {
        this.$refs['search-input'].blur()
      }, 300)
    }
  })
},

I think that for your case code should be like below:我认为您的案例代码应如下所示:

toggle(e) {
  if (e.key == 'g') {
    this.visible = !this.visible;
    setTimeout(() => { this.$refs.cInput.focus() }, 300)
  }
}

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

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