问答详情
源自:4-4 单页面应用 Demo2(1)

vuex 的mutations问题

之前讲的不是用  在mutations中  this.state.xxx吗;


为什么到这里变成了

addItem(state,value){

state.list.push(value)

}


另外我直接用

addItem(value){

this.state.list.push(value)

}

methods:{

add(){

store.commit('addItem',{

title:this.title,

content:this.content

})

}

},数组变成保存自己了??


提问者:将自己发配于人海 2019-01-17 15:16

个回答

  • Brian
    2019-01-21 13:00:07
    已采纳

    @将自己发配于人海 之前在vuex那一章中,使用this.state.count++是因为本身在mutations中,本身没有传入payload参数,可以使用this代表着本Store对象,去改变state的值。


    另外一个问题,关于载荷传参的问题。

    同学你所说的这个问题,我们要来读一下源码:

    在源码的vuex/src/store.js中,417行:

    function registerMutation (store, type, handler, local) {
      const entry = store._mutations[type] || (store._mutations[type] = [])
      entry.push(function wrappedMutationHandler (payload) {
        handler.call(store, local.state, payload)
      })
    }

    默认需要传入2个参数,state与payload。

    https://img.mukewang.com/5c45516e0001ceab19941288.jpg

    参考资料:

    call方法

    提交载荷(Payload)

  • AllySu
    2019-01-18 22:13:07

    一定要有完整的案例,不然人家不知道你要表达什么意思,类似这样https://jsfiddle.net/chrisvfritz/50wL7mdz/