1. 程式人生 > 其它 >呼叫子元件中的方法,明明定義了還報“Cannot read properties of undefined (reading 'fullValidEvent')”

呼叫子元件中的方法,明明定義了還報“Cannot read properties of undefined (reading 'fullValidEvent')”

在修改頁面,在父元件中直接呼叫子元件中的方法,通過ref來呼叫。明明在子元件中定義了這個方法,還一直報undefined

if (this.form.receivingType == 0) {
          await this.$refs.editTable1.fullValidEvent()
        } else if (this.form.receivingType == 1) {
          await this.$refs.editTable2.fullValidEvent()
        } else if (this.form.receivingType == 2) {
          await this.$refs.editTable3.fullValidEvent()
        }

我就嘗試先確定存在這個元件才呼叫這個方法,果然這樣可以

if (this.form.receivingType == 0) {
          this.$refs['editTable1'] && await this.$refs['editTable1'].fullValidEvent()
        } else if (this.form.receivingType == 1) {
          this.$refs['editTable2'] && await this.$refs['editTable2'].fullValidEvent()
        } else if (this.form.receivingType == 2) {
          this.$refs['editTable3'] && await this.$refs['editTable3'].fullValidEvent()
        }