Cannot read properties of undefined (reading ‘focus‘)“
出现这个的原因是获取的元素没有出来 等同于undefined.foucus以ref获取元素为例this.$refs.ipt.focus()直接写该代码会报错解决办法一:在外面加定时器解决办法二:$nextTick<template><div><button v-if="isShow" @click="tabFn">按钮</button><inp
·
出现这个的原因是获取的元素没有出来 等同于undefined.foucus
以ref获取元素为例this.$refs.ipt.focus()
直接写该代码会报错
解决办法一:在外面加定时器
解决办法二:$nextTick
<template>
<div>
<button v-if="isShow" @click="tabFn">按钮</button>
<input ref="ipt" v-else type="text" />
</div>
</template>
<script>
export default {
data() {
return {
isShow: true,
};
},
methods:{
tabFn(){
this.isShow=false
// 方法一
// setTimeout(() => {
// this.$refs.ipt.focus()
// }, 500);
// 方法二
this.$nextTick(()=>{
this.$refs.ipt.focus()
})
}
}
};
</script>
<style>
</style>
更多推荐
已为社区贡献4条内容
所有评论(0)