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. docker鏡像push報錯2. dockerfile - [docker build image失敗- npm install]3. Docker for Mac 創建的dnsmasq容器連不上/不工作的問題4. angular.js - angular內容過長展開收起效果5. 在windows下安裝docker Toolbox 啟動Docker Quickstart Terminal 失敗!6. docker-machine添加一個已有的docker主機問題7. 關于docker下的nginx壓力測試8. python - Django表單Form.save()問題9. python小白基礎問題 關于while循環10. python - pyqt多線程問題實在不是很明白。。求解。。
