鍍金池/ 問(wèn)答/HTML5  HTML/ vue 如何監(jiān)聽事件動(dòng)態(tài)傳至給子組件

vue 如何監(jiān)聽事件動(dòng)態(tài)傳至給子組件

點(diǎn)擊后defaultEducation的值明明改變了,但props傳給子組件卻watch不到

父組件

<choice :text="checkdata" ref="choice" @getchoice="onChoice" @cancel="showToggleCancel" :checkedValue="defaultEducation"></choice>


computed: {
      geitfasa () {
        document.addEventListener('tap', function (event) {
          var focusTargetDom = event.target
          if (focusTargetDom.tagName.toLowerCase() === 'input' && focusTargetDom.getAttribute('name') === 'checkdata') {
            this.defaultEducation = focusTargetDom.value
            console.log(this.defaultEducation)
          }
        }, true)
      }
    }

子組件

<input type="radio" :value="item.id" :id="`checkbox_${index}`" :name="item.radioName" v-model="checkedVal" @tap.stop="hideCheck"/>
        <label :for="`checkbox_${index}`" :id="`check_${index}`">
          {{item.name}}
        </label>
        
 props: {
      text: {
        type: Array,
        default: []
      },
      checkedValue: {
        type: String,
        default: ''
      }
    },
    data () {
      return {
        checkedVal: ''
      }
    },       
 created () {
      this.checkedVal = this.checkedValue
      this.$watch('checkedValue', (newquery) => {
        console.log(newquery)
      })
    }       
        
回答
編輯回答
深記你

props傳遞是自帶監(jiān)聽的

如果沒(méi)改變 你可以 watch $route的變化

2017年10月9日 07:06
編輯回答
傻叼

計(jì)算屬性里面寫事件 沒(méi)看懂
有這種寫法嗎
再說(shuō)你傳值的代碼不貼上來(lái) 怎么看

2018年2月5日 17:40
編輯回答
壞脾滊

首先確認(rèn)一下你的defaultEducation是什么類型的,如果是數(shù)組和對(duì)象這樣寫,子組件確實(shí)是監(jiān)聽不到的

2018年4月7日 16:43
編輯回答
墨沫
methods: {
      addevent () {
        document.addEventListener('tap', this._currentData, true)
      },
      _currentData (event) {
        var focusTargetDom = event.target
        if (focusTargetDom.tagName.toLowerCase() === 'input' && focusTargetDom.getAttribute('name') === 'checkdata') {
          this.defaultEducation = focusTargetDom.value
        }
      }
}

找到問(wèn)題了 這樣就能傳值,但原理還沒(méi)理解

2017年4月23日 09:45