鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ Vue 里怎樣在 Render 中使用 $refs

Vue 里怎樣在 Render 中使用 $refs

使用的 iView Select 組件,組件有一個(gè)方法: clearSingleSelect() . 調(diào)用方法為 this.$refs.nameSelect.clearSingleSelect(), 經(jīng)過測(cè)試正常使用是沒問題的。現(xiàn)在想要在 render 函數(shù)里調(diào)用, 一直獲取不到 this.$refs.nameSelect ,報(bào) undefined

相關(guān)代碼

h('Select', {
    ref: 'nameSelect',
    props: {
        transfer: true,
        clearable: true,
    },
    on: {
        'on-change': (value) => {
            if (value === 'CLEAR') {
                this.$refs.nameSelect.clearSingleSelect()
            }
        },
    },
}, this.assembleOptions(h, this.options))

怎樣在 Render function 里面使用 $refs 呢?

補(bǔ)充:


this 指向是沒問題的,log(this.$refs) 能看到其他直接在 template 里的寫好的組件的 ref . 感覺問題應(yīng)該是當(dāng) render 函數(shù)把 Select 組件渲染以后,this.$refs 沒有動(dòng)態(tài)更新。 所以報(bào) undefined ,有沒有手動(dòng)去更新 this.$refs 的方法,或者直接獲取到 render 函數(shù)渲染出來的實(shí)例的方法?

回答
編輯回答
我不懂

應(yīng)該是this指向的問題

2017年8月2日 13:11
編輯回答
萌二代

感覺是你on里面箭頭函數(shù)的問題

2017年8月7日 18:36
編輯回答
撥弦

我也遇到了這個(gè)問題,請(qǐng)問找到解決方案了嗎?

2018年7月9日 13:02
編輯回答
生性

樓主這個(gè)問題解決了嗎 我也遇到這個(gè)問題

2017年8月5日 13:42
編輯回答
焚音

這種請(qǐng)求一般就幾種問題
this指向
nameSelect通過for為數(shù)組
異步問題$refs 只會(huì)在組件渲染完成之后

2017年7月6日 20:08
編輯回答
兮顏

這里有兩個(gè)問題.

一個(gè)是你這里使用了箭頭函數(shù)改變了this指向,從運(yùn)行時(shí)變成了定義時(shí)。

二是在render里面獲取this.$refs這種操作只能獲取上次渲染的dom節(jié)點(diǎn),這確定是你想要的效果嗎?

2017年2月8日 12:56