鍍金池/ 問答/網(wǎng)絡(luò)安全  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()

        }
    

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

clipboard.png

回答
編輯回答
掛念你
  1. validateXX 是不是在調(diào)用之后引入的,或者定義的;
  2. 檢查是否命名出錯。
2018年5月20日 20:07
編輯回答
尤禮

可能是個數(shù)組,你先打出來看看

2017年3月29日 15:18
編輯回答
眼雜

在v-for循環(huán)中的自定義組件 在他身上放ref 取出來都是數(shù)組 故而沒有必要放動態(tài)值 比如ref="foo" 然后this.$refs.foo取得這個自定義組件的數(shù)組 然后再根據(jù)索引下標(biāo)去取得你要操作的組件實(shí)例。 取得實(shí)例之后就可以隨便調(diào)用實(shí)力方法

2017年3月22日 21:20
編輯回答
獨(dú)特范

控制臺要輸出方法的返回值,你調(diào)用的也應(yīng)該是方法,不應(yīng)該是這樣嗎?
console.log(result.validateXX())

2018年9月6日 06:00