Why can't Vue $refs get the component object - Stack Overflow
为情所困
为情所困 2017-07-05 10:51:01
0
4
1218
<el-tree ref="permissions_tree" class="permissions-tree" :data="permissions" :props="basicConfig.defaultProps" show-checkbox node-key="id" :render-content="renderNode"></el-tree>
mounted () {
  console.log(this.$refs.permissions_tree);
}

The printed output in mounted is undefined! why is that?

The button I rendered in the table, the first time I clicked it and called console.log(this.$refs.permissions_tree); I got undefined, but the second time I can get the component normally

{
    title: '操作',
    key: 'action',
    align: 'center',
    render: (h, params) => {
      return h('p', [
        h('Button', {
          props: {
            type: 'primary',
            size: 'small'
          },
          style: {
            marginRight: '5px'
          },
          on: {
            click: () => {
              this.userForm.staffid = params.row.staffid;
              this.userForm.name = params.row.name;
              this.userForm.phoneticize = params.row.phoneticize;
              this.userForm.gender = params.row.gender;
              this.userForm.mobile = params.row.mobile;
              this.userForm.telephone = params.row.telephone;
              this.userForm.identification = params.row.identification;
              this.userForm.positions = params.row.positions;
              this.userForm.permissions = params.row.permissions;
              this.userFormShow = true;
              console.log(this.$refs.permissions_tree); //这里
            }
          }
        }, '编辑')
      ]);
    }
}

为情所困
为情所困

reply all(4)
刘奇

Maybe you use v-if to switch component display, so you have to get it in the next tick

this.$nextTick(() => {
    console.log(this.$refs.permissions_tree);
});
扔个三星炸死你

Written in

this.$nextTick(() => {})
Give it a try in

大家讲道理

Do you use v-if in the outer component? Change it to v-show and give it a try

为情所困

Call this method
this.$nextTick(function () {

    // 里面打印
  })
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template