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. 模板渲染时报错,怎么处理2. node.js - 使用mongoose的cursor的问题3. javascript - nodemailer连接超时,怎么解决?4. Java。比较字符串时忽略重音5. mysql - Access denied for user ’ODBC’@’localhost’ (using password: NO)6. 请问一下,图片上传成功,但是后台对应文件夹里面却没有图片,这是什么原因?(已部署到服务器)7. 在别的电脑使用JDBC连接其他电脑上安装的mysql数据库8. PHP中的$this代表当前的类还是方法?9. mysql - 订单表,我把它分成好几个,我要查询订单,怎么用一条sql语句高效查询10. php - MySQL数据库设计,获取点赞的人数

网公网安备