鍍金池/ 問答/HTML/ Cannot read property 'setCheckedKeys' of

Cannot read property 'setCheckedKeys' of undefined

使用了element-ui 的Dialog 對話框,對話框中有Tree 樹形控件可以選擇樹節(jié)點的,當(dāng)我第一次打開Dialog并初始化Tree 樹形控件的節(jié)點就報錯,Cannot read property 'setCheckedKeys' of undefined,然后第二次打開就不會報錯,還能正常渲染數(shù)據(jù),請問要如何解決

回答
編輯回答
奧特蛋

思路大概這樣:

// 父組件
<button @click="update"></button>
<v-tree-dialog :is-show="isShow" ref="treeUpdate" ></v-tree-dialog> 

update() {
  this.isShow= true
  this.$nextTick(() => {
    this.$refs.treeUpdate.init()
  })
}
// 子組件
<el-tree
  :data="treeData"
  show-checkbox
  node-key="id"
  ref="tree"
>
</el-tree>

init() {
 this.$refs.tree.setCheckedKeys([1])
},
2018年9月9日 10:59