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. python - 啟動(dòng)Eric6時(shí)報(bào)錯(cuò):’qscintilla_zh_CN’ could not be loaded2. php - 微信開發(fā)驗(yàn)證服務(wù)器有效性3. MySQL中的enum類型有什么優(yōu)點(diǎn)?4. android下css3動(dòng)畫非常卡,GPU也不差啊5. mysql - 記得以前在哪里看過(guò)一個(gè)估算時(shí)間的網(wǎng)站6. css3 - 純css實(shí)現(xiàn)點(diǎn)擊特效7. javascript - 關(guān)于<a>元素與<input>元素的JS事件運(yùn)行問(wèn)題8. javascript - vue 怎么渲染自定義組件9. python - 有什么好的可以收集貨幣基金的資源?10. html - vue項(xiàng)目中用到了elementUI問(wèn)題
