鍍金池/ 問(wèn)答/HTML/ vue 調(diào)用父組件調(diào)用子組件的問(wèn)題

vue 調(diào)用父組件調(diào)用子組件的問(wèn)題

<income :getStandards="getStandards" :index="index" ref="refIncome"></income>

clickPurchase: function (index) {

   this.$refs.refIncome.handleStatePromptContent()

}
這是父組件引用的子組件

handleStatePromptContent () {

  if ((this.$parent.details.isFirst === 0) && (this.$parent.details.isNovice === 1)) {
  // this.showDialog = true
  // this.message = '僅限新手投資'
  this.statePromptContent = '僅限新手投資'
  return false
  }
  if ((this.$parent.details.isNovice === 1) && (this.Total > 50000)) {
  // this.showDialog = true
  this.statePromptContent = '新手標(biāo)投資不能超過(guò)50000'
  // this.message = '新手標(biāo)投資不能超過(guò)50000'
  return false
  }
  if (this.count <= 0) {
  // this.showDialog = true
  this.statePromptContent = '請(qǐng)選擇投資份數(shù)'
  // this.message = '請(qǐng)選擇投資份數(shù)'
  return false
  }
}
這里是子組件的方法

clipboard.png
然后就報(bào)這個(gè)錯(cuò)誤。求教是怎么回事。

回答
編輯回答
菊外人

handleStatePromptContent這個(gè)是組件里面的方法,this.$refs.refIncome指的是一個(gè)dom對(duì)象,dom對(duì)象怎么能去調(diào)用組件的方法呢

2017年3月19日 11:44
編輯回答
憶往昔

建議你不要用這種方式父組件去調(diào)用子組件的方法。建議用自定義事件,就是子組件監(jiān)聽(tīng)在mounted鉤子函數(shù)里面this.$on監(jiān)聽(tīng)一個(gè)事件,然后再在父組件需要的時(shí)候去this.$emit觸發(fā)那個(gè)事件。
還有,父子組件之間傳值最好用規(guī)矩一點(diǎn)的方式,不要直接this.$parent.details.isNovice這樣去獲取,vue是數(shù)據(jù)驅(qū)動(dòng)的,而不是直接去操作dom。

2017年10月25日 07:14
編輯回答
祈歡

用這種方式調(diào)用子組件的方法是沒(méi)問(wèn)題的,但是報(bào)錯(cuò)了,是不是你的子組件方法沒(méi)有正確放在methods里呢,推薦你去跟一下斷點(diǎn),先看看this.$refs.refIncome是否指向的是對(duì)應(yīng)子組件的vue實(shí)例對(duì)象(里面的一些屬性能幫助你確認(rèn)),然后再看看這個(gè)實(shí)例對(duì)象里是否有該方法。

2017年9月20日 18:39