javascript - Vuex 中状态发生变化时如何调度操作?

标签 javascript vue.js

我希望在状态发生变化时在 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]
})

关于javascript - Vuex 中状态发生变化时如何调度操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52252596/

相关文章:

javascript - Google map 距离矩阵 API 舍入 JS API v3

javascript - 根据所选框单击提交按钮时网址未打开

javascript - 使用 Select2 Multiple - 与 vueJS2

vue.js - _mapGetters() : getters should be function 的 Vuex 模块问题

javascript - 在外部选项卡 url 应用程序中访问 Teams session 和其他信息

javascript - Google Chrome Jquery onSelect 不会运行

javascript - .toggle() 在 figcaption 上不起作用

javascript - 鼠标悬停导航链接不起作用

javascript - 如何在 VueJS 中创建启动屏幕?

typescript - vue-router 3.5.1 : <router-link>'s tag prop is deprecated and has been removed in Vue Router 4 中的警告