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!這是為什么?
我在表格中渲染的按鈕,第一次點擊調(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); //這里 } }}, ’編輯’) ]); }}

問題解答
回答1:可能你用v-if來切換組件展示,所以要在下一個tick才能獲取到
this.$nextTick(() => { console.log(this.$refs.permissions_tree);});回答2:
寫在
this.$nextTick(() => {})
里試一下
回答3:外層組件是不是使用了v-if,換成v-show 試一下
回答4:調(diào)用這個方法this.$nextTick(function () {
// 里面打印 })
相關(guān)文章:
1. debian - docker依賴的aufs-tools源碼哪里可以找到啊?2. dockerfile - 我用docker build的時候出現(xiàn)下邊問題 麻煩幫我看一下3. docker網(wǎng)絡(luò)端口映射,沒有方便點的操作方法么?4. docker-compose 為何找不到配置文件?5. macos - mac下docker如何設(shè)置代理6. docker start -a dockername 老是卡住,什么情況?7. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個是怎么回事????8. 如何解決Centos下Docker服務(wù)啟動無響應(yīng),且輸入docker命令無響應(yīng)?9. angular.js使用$resource服務(wù)把數(shù)據(jù)存入mongodb的問題。10. docker gitlab 如何git clone?

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