computed property set not called in Vue

后端 未结 6 2184
没有蜡笔的小新
没有蜡笔的小新 2021-01-11 16:19

According to the documentation I should be able to use computed properties as v-model in Vue as long as I define get/set methods, but in my case it doesn\'t wor

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-11 16:59

    The very simple explanation here in code. computed properties are dependent on other data/reactive variables. If only when the reactive properties changed their values and if same property used to compute some other computed properties then the computed property would become reactive.

    this way we must set values and get in setter and getter methods.

    new Vue({
      el: '#app',
      data: {
        message: 'Use computed property on input',
        foo:0,
        isChecked:true
      },
      computed:{
       bar:{
        get: function(){
            return this.foo;
        },
       set: function(val){
         this.foo = val;
        }
       },
       
        check:{
        get: function(){
            return this.isChecked;
        },
       set: function(val){
         this.isChecked = val;
        }
       }
      }
    })
    
    
    

    {{ message }} Text

    {{bar}}

    {{ message }} Checkbox

    {{check}}

提交回复
热议问题