模式自动对焦上的 bootstrap vue 输入

Jim*_*hoe 5 vue.js bootstrap-vue

我在模态上有一个 bootstrap-vue 输入

<b-form-input  id="inputText1" ref="inputText1" v-model="inputText" autofocus></b-form-input>
Run Code Online (Sandbox Code Playgroud)

该模态是 bootstrap-vue 模态,显示/隐藏由 v-if 指令控制。

当模式打开时,输入具有焦点。如果我关闭模式,输入将不再具有焦点。

我尝试在每次安装模式时设置自动对焦属性,但它仍然不聚焦。我也尝试过使用 $nextTick 。

Sab*_*bee 5

我建议您v-model与 vue bootstrap modal 一起使用

模板

    <template>
      <div>
        <b-button @click="showModal= !showModal">Open your bmodal</b-button>
        <b-modal v-model="showModal">Yor modal is active!</b-modal>
        <b-form-input  id="inputText1" ref="inputText1" v-model="inputText" autofocus></b-form-input>

      </div>
    </template>
Run Code Online (Sandbox Code Playgroud)

vue代码

    new Vue({
      el: '#app',
       data() {
      return {
        showModal: false
      }
    },
  watch:{
    showModal:function(value){
     // set the focus when the modal opened/closed
      this.$refs.inputText1.focus();
    }
  },
      mounted(){
        // set the focus when the component opened
         this.$refs.inputText1.focus();
      },
      methods: {

      }
    });
Run Code Online (Sandbox Code Playgroud)