vue在computed中打印this.$refs为undefined
情景:在父组件的computed中打印this.$refs.content为undefined解决办法:首先绑定ref组件中必须是v-show, 因为this.$refs建立在dom渲染完毕的基础上。必须保证dom渲染完成。方法一data: {isMounted: false,},mounted() {this.isMounted = true},computed: ...
·
情景:在父组件的computed中打印this.$refs.content为undefined
解决办法:
- 首先绑定ref组件中必须是v-show, 因为this.$refs建立在dom渲染完毕的基础上。
- 必须保证dom渲染完成。
方法一
data: {
isMounted: false,
},
mounted() {
this.isMounted = true
},
computed: {
if(this.isMounted) {
console.log(this.$ref.content)
}
}
方法二
computed: {
this.$nextTick(() => {
console.log(this.$ref.content)
})
}
这两种方法都可以成功获取this.$refs里面的内容
更多推荐



所有评论(0)