vue中让input框自动聚焦

版权声明:转载请注明来源 https://blog.csdn.net/weixin_41829196/article/details/88355367
created(){
   this.changfouce();
  },
  methods: {
    //在vue生命周期的created()钩子函数进行的DOM操作要放在Vue.nextTick()的回调函数中,
    //因为created()钩子函数执行的时候DOM并未进行任何渲染,而此时进行DOM操作是徒劳的,所以此处一定要将DOM操作的JS代码放进Vue.nextTick()的回调函数中。
    changfouce(){
    this.$nextTick((x)=>{   //正确写法
       this.$refs.inputs.focus();
    })
     this.$refs.inputs.focus(); //错误写法
     
    },

猜你喜欢

转载自blog.csdn.net/weixin_41829196/article/details/88355367
今日推荐