computed property set not called in Vue

后端 未结 6 2152
没有蜡笔的小新
没有蜡笔的小新 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 17:15

    It seems the problem is both in the presence of options and the return value of the getter.

    You could try this:

    let options;
    
    try {
      options = JSON.parse(localStorage.getItem("options"));
    }
    catch(e) {
      // default values
      options = { test: true };
    }
    
    function saveOptions(updates) {
      localStorage.setItem("options", JSON.stringify({ ...options, ...updates }));
    }
    
    export default{
      template: `
        
    `, computed: { test: { get() { console.log('get'); return options.test; }, set(value) { console.log('set', value); saveOptions({ test: value }); }, }, } }

    Hope this helps.

提交回复
热议问题