在之前也演示過了mutation的基本使用,里面是定義一系列的函數,但函數的組成部分是有講究的,如下:
因此,在commit提交的時候,參數1是事件類型。那如何給mutation的方法傳入參數呢?也挺簡單的,如下:
傳入字符串或常量時:
<template> <div> <button @click="additionfive">counter+5</button> </div> </template> methods: { additionfive(){ const five = 5 this.$store.commit('increfive',five) } }
mutations: { increfive(state,five){ state.counter+=five } }
傳入json對象時:
<template> <div> <button @click="addstu">添加學生</button> </div> </template> methods: { addstu(){ const stu = {name: 'huya',age: 26} this.$store.commit('increstu',stu) } }
mutations: {
increstu(state,stu){
state.stus.push(stu)
}
}
說到這里要提及一個專業名詞,我們給mutations中的函數傳入的參數叫做:負載。