您的位置:首頁技術文章
文章詳情頁

javascript - Vue $refs 為什么無法獲取組件對象

瀏覽:96日期:2023-03-16 11:21:25

問題描述

<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); //這里 } }}, ’編輯’) ]); }}

javascript - Vue $refs  為什么無法獲取組件對象

問題解答

回答1:

可能你用v-if來切換組件展示,所以要在下一個tick才能獲取到

this.$nextTick(() => { console.log(this.$refs.permissions_tree);});回答2:

寫在

this.$nextTick(() => {})

里試一下

回答3:

外層組件是不是使用了v-if,換成v-show 試一下

回答4:

調用這個方法this.$nextTick(function () {

// 里面打印 })

標簽: JavaScript
相關文章:
国产综合久久一区二区三区