簡體   English   中英

Vuex中狀態變化時如何調度動作?

[英]How to dispatch action whenever state changes in Vuex?

每當狀態改變時,我希望在vuex中部署一些動作。 我能想到的唯一方法是將觀察者置於該狀態並從那里進行部署,但是我不確定這是否是最佳方法。 有一些更優雅的方法嗎?

您應該使用Vuex插件。 https://vuex.vuejs.org/guide/plugins.html

const myPlugin = store => {
  // called when the store is initialized
  store.subscribe((mutation, state) => {
    // called after every mutation.
    // The mutation comes in the format of `{ type, payload }`.
  })
}

這是處理您所詢問的此類問題的最佳和推薦方法。

定義插件時, 請記住在商店中注冊!

const store = new Vuex.Store({
  // ...
  plugins: [myPlugin]
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM