Actions
- action中可以通過呼叫 mutation來修改state,而不是直接變更狀態,
- action 可以包含任意異步(例如ajax請求)操作,
actions的定義格式
new Vuex.store({
// 省略其他...
actions: {
// context物件會自動傳入,它與store實體具有相同的方法和屬性
action的名字: function(context, 載荷) {
// 1. 發異步請求, 請求資料
// 2. commit呼叫mutation來修改/保存資料
// context.commit('mutation名', 載荷)
}
}
})
呼叫格式
在組件中通過this.$store.dispatch('actions的名字', 引數)來呼叫action
actions的執行程序
- 通過$store.dispatch(“actions名稱”)觸發actions
- actions中發起ajax異步請求,獲取服務器回應資料
- 使用commit呼叫mutations更改state里的資料
- 將state里指定的資料渲染到Vue組件

Map函式
使用規則:
- 在組件中引入map函式 import { mapState,mapxxx.... } from “vuex”
- 具體map使用流程如下

- map函式規則如下

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/303895.html
標籤:其他
