Set focus on input after it showed v-show

I have a simple form that is hidden when loading a page using v-show. I want to focus the input after it is shown. I have a button for calling a method that shows the form and sets focus on the input using this code:

    this.newQuestion = true; //(Form whit v-show:newQuestion)
    this.$refs.questionInput.focus();

The problem is that the form is displayed correctly, but the input is not focused the first time I click the button, if I click it the second time the form is on the page on which it works. I want to know if there is a way to do this, thanks.

+3
source share
2 answers

, ( , focus) Vue nextTick, , , DOM.

this.$nextTick(() => {
    this.$refs.questionInput.focus();
})
+5

:

var me = this;
Vue.nextTick(function() {
  me.$refs.questionInput.focus();
});
0

Source: https://habr.com/ru/post/1622488/


All Articles