鍍金池/ 問答/HTML/ VUE 如何點(diǎn)擊按鈕將子組件的input框的v-model 清空

VUE 如何點(diǎn)擊按鈕將子組件的input框的v-model 清空

子組件是一個(gè)input框,我需要在父組件那里點(diǎn)擊按鈕,將子組件的input框清空。
用vuex該怎么做,我點(diǎn)擊清空按鈕后將vuex的state值清空,子組件綁定state的值,監(jiān)聽input的v-model,發(fā)現(xiàn)不起作用

這是子組件v-model綁定的值
clipboard.png

這是監(jiān)聽v-model值變化
clipboard.png

這是父組件點(diǎn)擊清空按鈕將vuex的值清空
clipboard.png

clipboard.png

回答
編輯回答
誮惜顏

<input v-model="getState2"/>

computed:{

getState2(){
    return this.$store.state.machineSearchInputValue
}

}

2017年2月24日 15:10
編輯回答
旖襯

status2 綁定在 computed 內(nèi)。

  computed: {
    state2 () {
      return this.$store.state.machineSearchInputValue
    }
  }
2018年5月11日 09:29
編輯回答
囍槑

你把store里面的state寫在data里面,實(shí)例化組件的時(shí)候能取到state的值(原生JS的值傳遞),但是沒有建立綁定關(guān)系,所以你的watch是監(jiān)聽不到store里面state的變化的,正確寫法:

<template>
  <input type="text" v-model="cp_val">
</template>
// export default 中應(yīng)該綁定一個(gè)計(jì)算屬性
computed: {
      cp_val: {
        get() {
          return this.$store.state.value
        },
        set(v) {
          this.$store.commit('m_change', v)
        }
      }
    }
    
   // 父級要修改store的值,直接commit就可以了
 this.$store.commit('m_change', '')
2017年5月13日 10:33