简体   繁体   English

Vuex 4 + Vue 3 在模块中使用共享 state 动作

[英]Vuex 4 + Vue 3 using shared state actions in modules

I have a Vue 3 app (without TypeScript), and I'm using a Vuex 4 store.我有一个 Vue 3 应用程序(没有 TypeScript),并且我使用的是 Vuex 4 商店。

I have my store separated into modules that represent some common themes, for example I have a user module that contains the store, getters, actions etc that are appropriate for dealing with a user.我将商店分成代表一些常见主题的模块,例如,我有一个user模块,其中包含适合与用户打交道的商店、getter、动作等。 But, I also have some common functionalities that is present in all of my modules, and it's an obvious place where I could simplify my modules and thin them out a bit.但是,我的所有模块中也有一些共同的功能,这是一个明显的地方,我可以简化我的模块并将它们精简一点。

Let's say for example that I have a generic set() mutator, like below:例如,假设我有一个通用的set() mutator,如下所示:

const mutations = {
  set(state, payload) {
    state[payload.key] = payload.data;
  }
}

This will simply take in a payload and populate whatever store object the payload 'key' field belongs to, and it works well when I want to simply set a single field in my store.这将简单地接收一个有效负载并填充有效负载'key'字段所属的任何存储 object,当我想在我的存储中简单地设置一个字段时它工作得很好。 Now, the issue is that this set() function is being duplicated in every single store module that I have since it's just a generic store mutation, and once the number of modules I reach increases a bit, you can imagine that it's quite wasteful and pointless to include this mutation every single time.现在,问题是这个set() function 在我拥有的每个商店模块中都被复制,因为它只是一个通用的商店突变,一旦我到达的模块数量增加了一点,你可以想象它是相当浪费的每次都包含这种突变是没有意义的。

I'm not sure how to implement this, however.但是,我不确定如何实现这一点。

My current main store file looks like this (simplified for the sake of the question):我当前的主存储文件如下所示(为了问题而简化):

// store/index.js

import { createStore } from "vuex";

import module1 from "./modules/module1";
import module2 from "./modules/module2";

export const store = createStore({
    modules: { module1, module2 },
});

All of my modules are implemented in the same exact way:我所有的模块都以相同的方式实现:

// store/modules/module1.js

const state = { };
const mutations = { set(state, payload) };
const actions = { };
const getters = { };

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
};

And then somewhere in a component or wherever I'd call the specific module action/mutation/store I need with the below:然后在组件中的某个地方或任何我需要的特定模块操作/突变/存储的地方调用以下内容:

...mapMutations: ({ set: "module1/set" })

What would be the best way for me to bring that shared functionality out into a singular place, and how would I properly use it if I, for example, wanted to set and mutate the store in module1 and module2 at the same time properly?对我来说,将共享功能带到一个单独的地方的最佳方式是什么?例如,如果我module2同时正确地set和改变module1 1 和模块 2 中的存储,我将如何正确使用它? The part that I'm not sure about is how exactly I could call a generic mutation and have it target the desired module's state我不确定的部分是我如何准确地调用通用突变并让它针对所需模块的 state

Thanks to this answer linked by @Beyers, I've implemented the store in a similar fashion to this:感谢@Beyers 链接的这个答案,我以与此类似的方式实现了商店:

// store/sharedModuleMethods.js

export default class {
  constructor() {
    this.mutations = {
      set(state, payload) {}
    }
  }
}

And then in the modules, I just instantiate the class and spread the methods where they need to be然后在模块中,我只是实例化 class 并将方法传播到需要的地方

// store/modules/module1.js

import SharedModuleMethods from "../sharedModuleMethods";
const methods = new SharedModuleMethods()

const mutations = {
  ...methods.mutations,
  // Module specific mutations go here
};

And then in whatever component I need to set() something, I can just call the mutation like I would before然后在我需要set()的任何组件中,我可以像以前一样调用突变

...mapMutations: ({ setModule1: "module1/set", setModule2: "module2/set" })

It's not quite as automated and streamlined as I'd personally prefer (define it once, have the modules magically have all the methods available to them via a flag or something), but alas life isn't perfect either.它不像我个人喜欢的那样自动化和简化(定义一次,让模块神奇地通过标志或其他东西神奇地拥有所有可用的方法),但是生活也不是完美的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM