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. Span標簽2. css - 求推薦適用于vue2的框架 像bootstrap這種類型的3. docker-machine添加一個已有的docker主機問題4. css - 關于div自適應問題,大家看圖吧,說不清5. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””6. SessionNotFoundException:會話ID為null。調用quit()后使用WebDriver嗎?(硒)7. android新手一枚,android使用httclient獲取服務器端數據失敗,但是用java工程運行就可以成功獲取。8. angular.js使用$resource服務把數據存入mongodb的問題。9. java - Collections類里的swap函數,源碼為什么要新定義一個final的List型變量l指向傳入的list?10. python - django如何每次調用標簽的時候都取隨機數據
