鍍金池/ 問答/網(wǎng)絡安全  HTML/ vue中,通過refs調用子組件的方法為undefined

vue中,通過refs調用子組件的方法為undefined

有一個列表:

<ordiary-group
   v-for="(commonAttr, index) in warePublishData.commonAttrs"
   :ref="'ref_ordiary_group_' + index"
   :key="'ordiary_group_' + commonAttr.propertyId"
   :property-id="commonAttr.propertyId"
   :attribute-type="commonAttr.inputType"
   :attribute="commonAttr"
   :checked-list="getCheckedList(commonAttr.propertyId)"
   @validate-ordinary="validateOrdinary">
</ordiary-group>

然后我需要獲取每個列表的refs,如下:

    validate() {
            let _self = this
            let validate = () => {
                let passFlag = true
                for (let i = 0, len = this.warePublishData.commonAttrs.length; i < len; i++) {
                    let result = _self.$refs['ref_ordiary_group_' + i]
                    _self.$nextTick(() => {
                        console.log(result)
                        console.log(result.validateXX)
                    })
                }
            }
            return validate()

        }
    

打印出組件的內容有validateXX方法,但是獲取的時候卻是undefined,請問是什么原因?

clipboard.png

回答
編輯回答
孤巷

$nextTick(()=>{
let result = _self.$refs['ref_ordiary_group_' + i]
...
})

2018年3月23日 21:40
編輯回答
下墜

你在哪個時期執(zhí)行的 ? mounted才開始獲取到refs的。

2017年10月28日 11:45
編輯回答
離魂曲

我之前碰到的問題是:我的項目中使用了事件總線,來回切換路由出現(xiàn)了這個問題,解決辦法:
在路由跳轉頁面加上:

destroyed() {
    this.$event.$off()
}
2017年12月31日 18:34
編輯回答
夢一場

節(jié)點還沒有生成。加個定時器試試

2017年12月15日 18:22