Vuex - Computed property “name” was assigned to but it has no setter

后端 未结 4 1370
耶瑟儿~
耶瑟儿~ 2020-12-07 11:41

I have a component with some form validation. It is a multi step checkout form. The code below is for the first step. I\'d like to validate that the user entered some text,

相关标签:
4条回答
  • 2020-12-07 12:10

    It should be like this.

    In your Component

    computed: {
            ...mapGetters({
                    nameFromStore: 'name'
                }),
            name: {
               get(){
                 return this.nameFromStore
               },
               set(newName){
                 return newName
               } 
            }
        }
    

    In your store

    export const store = new Vuex.Store({
             state:{
                 name : "Stackoverflow"
             },
             getters: {
                     name: (state) => {
                         return state.name;
                     }
             }
    }
    
    0 讨论(0)
  • 2020-12-07 12:16

    For me it was changing.

    this.name = response.data;
    

    To what computed returns so;

    this.$store.state.name = response.data;
    
    0 讨论(0)
  • 2020-12-07 12:16

    I was facing exact same error

    Computed property "callRingtatus" was assigned to but it has no setter

    here is a sample code according to my scenario

    computed: {
    
    callRingtatus(){
                return this.$store.getters['chat/callState']===2
          }
    
    }
    

    I change the above code into the following way

    computed: {
    
    callRingtatus(){
           return this.$store.state.chat.callState===2
        }
    }
    
    

    fetch values from vuex store state instead of getters inside the computed hook

    0 讨论(0)
  • 2020-12-07 12:18

    If you're going to v-model a computed, it needs a setter. Whatever you want it to do with the updated value (probably write it to the $store, considering that's what your getter pulls it from) you do in the setter.

    If writing it back to the store happens via form submission, you don't want to v-model, you just want to set :value.

    If you want to have an intermediate state, where it's saved somewhere but doesn't overwrite the source in the $store until form submission, you'll need to create such a data item.

    0 讨论(0)
提交回复
热议问题