Vue使用中遇到的代码问题

Computed property "xxx" was assigned to but it has no setter.

@/store/modules/user.js

export default {
   state: {
       msg: '123456'
   }
}

@/store/index.js

import Vue from 'vue'
import vuex from 'vuex'
import user from './modules/user'
Vue.use(vuex)
export default new vuex.Store({
    modules: {
        user
    }
})

@/components/HelloWorld.vue

        computed: {
            ...mapState({
                msg: state => state.user.msg,
                user: state => state.user
            })
        },
        methods: {
            onChange: function () {
                this.msg = 'abc' // 这一行有问题
                this.user = {  // 这一行也有问题
                    msg: 'def'
                }
                this.user.msg = '123'  // 这一行没有问题
            }
        }

image.png

user 会默认有类似setMsg函数,而this不会默认有setMsg或者setUser函数,所以会报错。
参考:https://blog.csdn.net/cominglately/article/details/81063065

你可能感兴趣的:(Vue使用中遇到的代码问题)