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. 如何解決Centos下Docker服務啟動無響應,且輸入docker命令無響應?2. 我在centos容器里安裝docker,也就是在容器里安裝容器,報錯了?3. 微信小程序session無法緩存的問題4. 怎么用 css3實現波浪底紋效果?5. vue.js - 關于Vue-cli項目在VPS中用Nginx部署完請求頁面app.js找不到提示404錯誤。6. docker 17.03 怎么配置 registry mirror ?7. html - 這種錯位的時間軸怎么布局,然后用css實現?8. 基于Nginx的Wordpress安裝失敗?9. css3 - 何時需要 flex-basis: 100% ?10. mysqld無法關閉
