vue3报错:this.$refs.** undefined

背景:异步加载数据回填到this.refs.\*\* 中,浏览器报错找不到 this.refs.** 。

进过阅读资料,提到是组件未初始化,意思是页面没加载出来前, this.$refs是无法使用的。

解决:

先加载页面

javascript 复制代码
 <el-dialog :title="title" :visible.sync="open" width="75%" class="cust-task-index"  append-to-body>
      <link-test-history-table  ref="historyTable" />
    </el-dialog>

比如我的对话框是用变量open控制,那么在调用this.$refs.** 前,设置代码

this.open = true ,让对话框加载。

然后再对this.$refs.historyTable 赋值,解决改问题。