vuex 的mutations问题

来源:4-4 单页面应用 Demo2(1)

将自己发配于人海

2019-01-17 15:16

之前讲的不是用  在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

})

}

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


写回答 关注

2回答

  • 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)

    慕仔0302...

    那为什么这章节中使用this.state会有问题呢

    2020-10-29 16:50:20

    共 1 条回复 >

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

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

3小时速成 Vue2.x 核心技术

带你快速学习最流行的前端框架vue2.x的核心技术

82572 学习 · 489 问题

查看课程

相似问题