javascript - Vue $refs 为什么无法获取组件对象
问题描述
<el-tree ref='permissions_tree' :data='permissions' :props='basicConfig.defaultProps' show-checkbox node-key='id' :render-content='renderNode'></el-tree>
mounted () { console.log(this.$refs.permissions_tree);}
在 mounted 中打印输出的是undefined!这是为什么?
我在表格中渲染的按钮,第一次点击调用 console.log(this.$refs.permissions_tree);得到的也是 undefined,第二次就能正常获取到组件了
{ 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); //这里 } }}, ’编辑’) ]); }}

问题解答
回答1:可能你用v-if来切换组件展示,所以要在下一个tick才能获取到
this.$nextTick(() => { console.log(this.$refs.permissions_tree);});回答2:
写在
this.$nextTick(() => {})
里试一下
回答3:外层组件是不是使用了v-if,换成v-show 试一下
回答4:调用这个方法this.$nextTick(function () {
// 里面打印 })
相关文章:
1. javascript - 关于数组的循环遍历问题2. javascript - 正则匹配字符串特定语句后的数字3. 在cmd下进入mysql数据库,可以输入中文,但是查看表信息,不显示中文,是怎么回事,怎新手,请老师4. python - Pycharm调试代码进行列表遍历时,如何直接赋值指定元素5. javascript - 请指条明路,angular的$event,在select中却是undefined?6. java 线程池序号一直增加问题7. java - 重载是不是多态??8. mysql优化 - mysql 多表联合查询,求一个效率最高的查询9. python - 有一个函数名(字符串形式),如何能够调用这个函数?10. 微信支付 - python做微信企业付款出现CA证书错误

网公网安备