gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:16:35 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com