Vue使用中遇到的程式碼問題

weixin_34321977發表於2018-07-28

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'  // 這一行沒有問題
            }
        }

2159680-1b447c49a16a2a39.png
image.png

user 會預設有類似setMsg函式,而this不會預設有setMsg或者setUser函式,所以會報錯。
參考:https://blog.csdn.net/cominglately/article/details/81063065

相關文章