首页 > 技术文章 > 调用子组件中的方法,明明定义了还报“Cannot read properties of undefined (reading 'fullValidEvent')”

wang--chao 2021-11-29 16:50 原文

在修改页面,在父组件中直接调用子组件中的方法,通过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()
        }

image

我就尝试先确定存在这个组件才调用这个方法,果然这样可以

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()
        }

推荐阅读