鍍金池/ 問答/HTML/ TypeError: Cannot read property 'setChec

TypeError: Cannot read property 'setCheckedKeys' of undefined

在角色資源管理的時候 點擊設置資源 彈出資源列表的樹 ,并且賦上默認值,但是在第一次調(diào)用彈出層的方法,會報這個錯,第二次點擊的時候不會報這個錯 而且 getHalfCheckedKeys也使用不了

圖片描述

 handleSetResource(val) {
    this.Visible = true
    this.Status = 'setResource'
    const currRes = val.resources
    for(const v of currRes){
      console.log(v.id)
      this.$refs.tree.setCheckedKeys([v.id]);
    }
  },
  getHalfCheckedKeys() {
    console.log(this.$refs.tree.getHalfCheckedKeys());
  },
回答
編輯回答
毀與悔

在彈出dialog之前執(zhí)行$nextTick回調(diào)函數(shù),確保el-tree已經(jīng)渲染

this.visible = true;
this.$nextTick(() => {
    // code
});
2018年5月10日 13:31
編輯回答
夏夕

應該是第一次彈框的時候,樹還沒有加載完成,所以獲取不到,第二次的時候樹已經(jīng)加載完成,所以可以調(diào)用方法。

2018年8月5日 06:10