天天看點

如何使用v-model綁定computed的屬性

一、需求:

       在vue開發的時候,有時候需要v-model綁定computed計算屬性。而computed預設是隻顯示不更改屬性的。如何解決?見下文

二、實作

       思路:在computed相對應的屬性中設定get和set來對應v-model的雙向綁定

       代碼如下:

<template>
	<Modal v-model='mydata'></Modal>
</template>
...
computed: {
    mydata: {
      get () {
        return this.$store.state.mydata
      },
      set (val) {
        this.$store.commit('update', val)
      }
    },
}