javascript - Vue $refs 為什么無(wú)法獲取組件對(duì)象
問(wèn)題描述
<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!這是為什么?
我在表格中渲染的按鈕,第一次點(diǎn)擊調(diào)用 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); //這里 } }}, ’編輯’) ]); }}

問(wèn)題解答
回答1:可能你用v-if來(lái)切換組件展示,所以要在下一個(gè)tick才能獲取到
this.$nextTick(() => { console.log(this.$refs.permissions_tree);});回答2:
寫在
this.$nextTick(() => {})
里試一下
回答3:外層組件是不是使用了v-if,換成v-show 試一下
回答4:調(diào)用這個(gè)方法this.$nextTick(function () {
// 里面打印 })
相關(guān)文章:
1. golang - 用IDE看docker源碼時(shí)的小問(wèn)題2. html - css氣泡,實(shí)現(xiàn)“倒三角(不知道算不算三角了)”可透明的。3. docker images顯示的鏡像過(guò)多,狗眼被亮瞎了,怎么辦?4. 運(yùn)行python程序時(shí)出現(xiàn)“應(yīng)用程序發(fā)生異常”的內(nèi)存錯(cuò)誤?5. docker - 如何修改運(yùn)行中容器的配置6. phpstudy8.1沒(méi)集成mysql-front7. 我何時(shí)應(yīng)該在Java中使用JFrame.add(component)和JFrame.getContentPane()。add(component)8. 利用IPMI遠(yuǎn)程安裝centos報(bào)錯(cuò)!9. 前端 - @media query 使用出現(xiàn)的問(wèn)題?10. html5和Flash對(duì)抗是什么情況?

網(wǎng)公網(wǎng)安備