vue-vuex-mutation傳遞參數


  在之前也演示過了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中的函數傳入的參數叫做:負載。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM