Categories
Vue 3

Vuex 4 — Modules

Vuex 4 is in beta and it’s subject to change.

Vuex is a popular state management library for Vue.

Vuex 4 is the version that’s made to work with Vue 3.

In this article, we’ll look at how to use Vuex 4 with Vue 3.

Modules

We can divide our Vuex 4 store into modules so that we can divide our states into multiple smaller parts.

Each module has their own state, mutations, actions, and getters.

Module Local State

Each module has their own local state. We can map the module’s state into computed properties and mutations into methods by writing:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="increment">increment</button>
      <p>{{doubleCount}}</p>
    </div>
    <script>
      const moduleA = {
        state: () => ({
          count: 0
        }),
        mutations: {
          increment(state) {
            state.count++;
          }
        },
        getters: {
          doubleCount(state) {
            return state.count * 2;
          }
        }
      };

      const store = new Vuex.Store({
        modules: {
          moduleA
        }
      });
      const app = Vue.createApp({
        methods: {
          ...Vuex.mapMutations(["increment"])
        },
        computed: {
          ...Vuex.mapGetters(["doubleCount"])
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

We created the store with the modules property to include our module in the store.

Then in our Vue instance, we have the mapMutations and mapGetters methods to map the mutations to methods and the getters to computed properties.

We can do the same with actions and states.

For example, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="increment">increment</button>
      <p>{{moduleA.count}}</p>
    </div>
    <script>
      const moduleA = {
        state: () => ({
          count: 0
        }),
        mutations: {
          increment(state) {
            state.count++;
          }
        },
        actions: {
          increment({ commit }) {
            commit("increment");
          }
        }
      };

      const store = new Vuex.Store({
        modules: {
          moduleA
        }
      });
      const app = Vue.createApp({
        methods: {
          ...Vuex.mapActions(["increment"])
        },
        computed: {
          ...Vuex.mapState({ moduleA: (state) => state.moduleA })
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

We called the mapActions method with an array of action names.

The mapState method is called with an object with methods to return the module states.

The states are in the object with the module name as the property name.

Namespacing

We can namespace our modules.

This way, we won’t have everything under one global namespace.

Also, this lets multiple modules react to the same mutation or action type.

For instance, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="this['moduleA/increment']">increment</button>
      <p>{{moduleA.count}}</p>
    </div>
    <script>
      const moduleA = {
        state: () => ({
          count: 0
        }),
        mutations: {
          increment(state) {
            state.count++;
          }
        },
        actions: {
          increment({ commit }) {
            commit("increment");
          }
        }
      };

      const store = new Vuex.Store({
        modules: {
          moduleA: {
            namespaced: true,
            ...moduleA
          }
        }
      });
      const app = Vue.createApp({
        methods: {
          ...Vuex.mapActions(["moduleA/increment"])
        },
        computed: {
          ...Vuex.mapState({ moduleA: (state) => state.moduleA })
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

We mapped the action with the namespace name since we set the namespaced property to true .

We can do the same with getters and mutations.

To do that, we write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="this['moduleA/increment']">increment</button>
      <p>{{this['moduleA/doubleCount']}}</p>
    </div>
    <script>
      const moduleA = {
        state: () => ({
          count: 0
        }),
        mutations: {
          increment(state) {
            state.count++;
          }
        },
        getters: {
          doubleCount(state) {
            return state.count;
          }
        }
      };

      const store = new Vuex.Store({
        modules: {
          moduleA: {
            namespaced: true,
            ...moduleA
          }
        }
      });
      const app = Vue.createApp({
        methods: {
          ...Vuex.mapMutations(["moduleA/increment"])
        },
        computed: {
          ...Vuex.mapGetters(["moduleA/doubleCount"])
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

We called mapMutations and mapGetters with the namespace name since we set namespaced to true in the module.

Conclusion

We can create our modules and namespace them so that we can have multiple smaller parts of a store that manage their own state.

Categories
Vue 3

Vuex 4 — Dynamic Module Registration and Logging

Vuex 4 is in beta and it’s subject to change.

Vuex is a popular state management library for Vue.

Vuex 4 is the version that’s made to work with Vue 3.

In this article, we’ll look at how to use Vuex 4 with Vue 3.

Dynamic Module Registration

We can register modules after the store it’s created with the store.registerModule method.

For example, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="increment">increment</button>
      <p>{{doubleCount}}</p>
    </div>
    <script>
      const moduleA = {
        state: () => ({
          count: 0
        }),
        mutations: {
          increment(state) {
            state.count++;
          }
        },
        getters: {
          doubleCount(state) {
            return state.count * 2;
          }
        }
      };

      const store = new Vuex.Store({});

      store.registerModule("moduleA", moduleA);
      const app = Vue.createApp({
        methods: {
          ...Vuex.mapMutations(["increment"])
        },
        computed: {
          ...Vuex.mapGetters(["doubleCount"])
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

We called the store.registerModule method with our module name and object to register the module.

Then we can use it in our components like any other module.

Plugins

We can create and use plugins in our store.

To do this, we just create a function that takes the store parameter and do something with it in the function

For example, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="increment">increment</button>
      <p>{{doubleCount}}</p>
    </div>
    <script>
      const myPlugin = (store) => {
        store.subscribe((mutation, state) => {
          console.log(state);
        });
      };

      const store = new Vuex.Store({
        state: () => ({
          count: 0
        }),
        mutations: {
          increment(state) {
            state.count++;
          }
        },
        getters: {
          doubleCount(state) {
            return state.count * 2;
          }
        },
        plugins: [myPlugin]
      });

      const app = Vue.createApp({
        methods: {
          ...Vuex.mapMutations(["increment"])
        },
        computed: {
          ...Vuex.mapGetters(["doubleCount"])
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

We called subscribe with a callback that has the mutation and state parameters.

mutation has the data of the mutation being committed.

state has the current state of the store.

Built-in Logger Plugin

A logged plugin is provided by Vuex 4.

To use it, we call the createLogger function.

For example, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="increment">increment</button>
      <p>{{doubleCount}}</p>
    </div>
    <script>
      const store = new Vuex.Store({
        state: () => ({
          count: 0
        }),
        mutations: {
          increment(state) {
            state.count++;
          }
        },
        getters: {
          doubleCount(state) {
            return state.count * 2;
          }
        },
        plugins: [Vuex.createLogger()]
      });

      const app = Vue.createApp({
        methods: {
          ...Vuex.mapMutations(["increment"])
        },
        computed: {
          ...Vuex.mapGetters(["doubleCount"])
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

to use it.

We called the Vuex.createLogger method to return the logged plugin, which we pass into the plugins array.

The method takes an object with some properties which we can use as options.

They are various filters.

For example, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="increment">increment</button>
      <p>{{doubleCount}}</p>
    </div>
    <script>
      const store = new Vuex.Store({
        state: () => ({
          count: 0
        }),
        mutations: {
          increment(state) {
            state.count++;
          }
        },
        getters: {
          doubleCount(state) {
            return state.count * 2;
          }
        },
        plugins: [
          Vuex.createLogger({
            collapsed: false,
            filter(mutation, stateBefore, stateAfter) {
              return mutation.type !== "foo";
            },
            actionFilter(action, state) {
              return action.type !== "foo";
            },
            transformer(state) {
              return state.subTree;
            },
            mutationTransformer(mutation) {
              return mutation.type;
            },
            actionTransformer(action) {
              return action.type;
            },
            logActions: true,
            logMutations: true,
            logger: console
          })
        ]
      });

      const app = Vue.createApp({
        methods: {
          ...Vuex.mapMutations(["increment"])
        },
        computed: {
          ...Vuex.mapGetters(["doubleCount"])
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

to set the actions.

filter lets us filter the mutation or state to log.

actionFilter lets us filter actions by action or state properties.

transformer lets us transform the log output.

mutationTransformer lets us transform mutations log output.

actionTransformer lets us transform our actions log output.

logActions is a boolean to let us log actions if it’s true .

logMutations lets us log mutations if it’s true .

logger lets us set the logger we want to use to do the logging.

Conclusion

We can register Vuex 4 store modules dynamically.

Also, Vuex 4 comes with its own logger.

Categories
Vue 3

Vuex 4 — Actions and Components

Vuex 4 is in beta and it’s subject to change.

Vuex is a popular state management library for Vue.

Vuex 4 is the version that’s made to work with Vue 3.

In this article, we’ll look at how to use Vuex 4 with Vue 3.

Dispatching Actions in Components

We can map our actions to component methods with the mapActions method.

For example, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="increment">increment</button>
      <p>{{count}}</p>
    </div>
    <script>
      const store = new Vuex.Store({
        state: {
          count: 0
        },
        mutations: {
          increment(state) {
            state.count++;
          }
        },
        actions: {
          increment({ commit }) {
            commit("increment");
          }
        }
      });
      const app = Vue.createApp({
        methods: {
          ...Vuex.mapActions(["increment"])
        },
        computed: {
          count() {
            return this.$store.state.count;
          }
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

We called the Vuex.mapActions to map our actions into a method.

We can also map it to our method with a name that’s different from the action.

For example, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="add">increment</button>
      <p>{{count}}</p>
    </div>
    <script>
      const store = new Vuex.Store({
        state: {
          count: 0
        },
        mutations: {
          increment(state) {
            state.count++;
          }
        },
        actions: {
          increment({ commit }) {
            commit("increment");
          }
        }
      });
      const app = Vue.createApp({
        methods: {
          ...Vuex.mapActions({ add: "increment" })
        },
        computed: {
          count() {
            return this.$store.state.count;
          }
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

We passed in an object to Vuex.mapActions to map the increment action to the add method.

The key is the method name we want to map to.

Composing Actions

Actions are often async. We can check that it’s done by returning a promise.

For example, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="increment2">increment</button>
      <p>{{count}}</p>
    </div>
    <script>
      const store = new Vuex.Store({
        state: {
          count: 0
        },
        mutations: {
          increment(state, amount) {
            state.count += amount;
          }
        },
        actions: {
          async increment({ commit }) {
            return commit("increment", 1);
          },
          async increment2({ commit }) {
            await commit("increment", 1);
            return commit("increment", 2);
          }
        }
      });
      const app = Vue.createApp({
        methods: {
          ...Vuex.mapActions(["increment2"])
        },
        computed: {
          count() {
            return this.$store.state.count;
          }
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

We have the increment2 action which dispatches the increment action with amount 1.

And then we dispatch it again with amount 2.

Since they’re promises and we awaited the first one, they’ll run sequentially.

Conclusion

We can dispatch actions in different ways with Vuex 4.

Actions can be mapped to methods with Vuex.mapActions .

Categories
Vue 3

Vuex 4 — Actions

Vuex 4 is in beta and it’s subject to change.

Vuex is a popular state management library for Vue.

Vuex 4 is the version that’s made to work with Vue 3.

In this article, we’ll look at how to use Vuex 4 with Vue 3.

Actions

In Vuex 4, actions are similar to mutations.

The differences are that actions commit mutations and actions can have async operations.

To create an action, we can add an actions property to our store.

For instance, we can write:

<!DOCTYPE html>  
<html lang="en">  
  <head>  
    <script src="https://unpkg.com/vue@next"></script>  
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>  
    <title>App</title>  
  </head>  
  <body>  
    <div id="app">  
      <button @click="increment">increment</button>  
      <p>{{count}}</p>  
    </div>  
    <script>  
      const store = new Vuex.Store({  
        state: {  
          count: 0  
        },  
        mutations: {  
          increment(state) {  
            state.count++;  
          }  
        },  
        actions: {  
          increment(context) {  
            context.commit("increment");  
          }  
        }  
      });  
      const app = Vue.createApp({  
        methods: {  
          increment() {  
            this.$store.dispatch("increment");  
          }  
        },  
        computed: {  
          count() {  
            return this.$store.state.count;  
          }  
        }  
      });  
      app.use(store);  
      app.mount("#app");  
    </script>  
  </body>  
</html>

We created a Vuex store with an actions property that has the increment method in it.

It takes the comtext parameter with the commit method to commit mutations.

To dispatch the action in our component, we call this.$store.dispatch with the action name.

We can perform async operations in an action.

For example, we can write:

<!DOCTYPE html>  
<html lang="en">  
  <head>  
    <script src="https://unpkg.com/vue@next"></script>  
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>  
    <title>App</title>  
  </head>  
  <body>  
    <div id="app">  
      <button @click="increment">increment</button>  
      <p>{{count}}</p>  
    </div>  
    <script>  
      const store = new Vuex.Store({  
        state: {  
          count: 0  
        },  
        mutations: {  
          increment(state) {  
            state.count++;  
          }  
        },  
        actions: {  
          incrementAsync({ commit }) {  
            setTimeout(() => {  
              commit("increment");  
            }, 1000);  
          }  
        }  
      });  
      const app = Vue.createApp({  
        methods: {  
          increment() {  
            this.$store.dispatch("incrementAsync");  
          }  
        },  
        computed: {  
          count() {  
            return this.$store.state.count;  
          }  
        }  
      });  
      app.use(store);  
      app.mount("#app");  
    </script>  
  </body>  
</html>

We called commit inside the setTimeout callback to delay the action dispatch by a second.

Also, we can dispatch actions with a payload.

For instance, we can write:

<!DOCTYPE html>  
<html lang="en">  
  <head>  
    <script src="https://unpkg.com/vue@next"></script>  
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>  
    <title>App</title>  
  </head>  
  <body>  
    <div id="app">  
      <button @click="increment">increment</button>  
      <p>{{count}}</p>  
    </div>  
    <script>  
      const store = new Vuex.Store({  
        state: {  
          count: 0  
        },  
        mutations: {  
          increment(state, payload) {  
            state.count += payload.amount;  
          }  
        },  
        actions: {  
          incrementAsync({ commit }, payload) {  
            setTimeout(() => {  
              commit("increment", payload);  
            }, 1000);  
          }  
        }  
      });  
      const app = Vue.createApp({  
        methods: {  
          increment() {  
            this.$store.dispatch("incrementAsync", { amount: 2 });  
          }  
        },  
        computed: {  
          count() {  
            return this.$store.state.count;  
          }  
        }  
      });  
      app.use(store);  
      app.mount("#app");  
    </script>  
  </body>  
</html>

We added a payload parameter to the incrementAsync action to accept payloads in our action.

Then we pass in an object as the 2nd argument to the this.$store.dispatch method to dispatch our action.

We can also pass in an object with the type property and arbitrary properties that will be included in the payload:

<!DOCTYPE html>  
<html lang="en">  
  <head>  
    <script src="https://unpkg.com/vue@next"></script>  
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>  
    <title>App</title>  
  </head>  
  <body>  
    <div id="app">  
      <button @click="increment">increment</button>  
      <p>{{count}}</p>  
    </div>  
    <script>  
      const store = new Vuex.Store({  
        state: {  
          count: 0  
        },  
        mutations: {  
          increment(state, payload) {  
            state.count += payload.amount;  
          }  
        },  
        actions: {  
          incrementAsync({ commit }, payload) {  
            setTimeout(() => {  
              commit("increment", payload);  
            }, 1000);  
          }  
        }  
      });  
      const app = Vue.createApp({  
        methods: {  
          increment() {  
            this.$store.dispatch({ type: "incrementAsync", amount: 2 });  
          }  
        },  
        computed: {  
          count() {  
            return this.$store.state.count;  
          }  
        }  
      });  
      app.use(store);  
      app.mount("#app");  
    </script>  
  </body>  
</html>

The type is the name of the action and the other properties will end up in the payload object in the 2nd argument of the incrementAsync action.

Conclusion

We can create actions to commit mutations and run async code with Vuex 4.

Categories
Vue 3

Vuex 4 — Mutations

Vuex 4 is in beta and it’s subject to change.

Vuex is a popular state management library for Vue.

Vuex 4 is the version that’s made to work with Vue 3.

In this article, we’ll look at how to use Vuex 4 with Vue 3.

Object-Style Commit

We can pass in an object to the commit method.

For example, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="decrement">decrement</button>
      <p>{{count}}</p>
    </div>
    <script>
      const store = new Vuex.Store({
        state: {
          count: 0
        },
        mutations: {
          decrement(state, payload) {
            state.count -= payload.amount;
          }
        }
      });
      const app = Vue.createApp({
        methods: {
          decrement() {
            this.$store.commit({
              type: "decrement",
              amount: 5
            });
          }
        },
        computed: {
          count() {
            return this.$store.state.count;
          }
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

We pass in an object into the this.$store.commit method.

Properties other than type would end up in the payload object.

So we can access the amount property with payload.amount in the decrement mutation method.

Mutations follow Vue 3’s reactivity rules, so whenever a reactive state variable is updated, the store state will change.

Using Constants for Mutation Types

We can use constants for mutation types.

For example, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="decrement">decrement</button>
      <p>{{count}}</p>
    </div>
    <script>
      const DECREMENT = "decrement";

      const store = new Vuex.Store({
        state: {
          count: 0
        },
        mutations: {
          [DECREMENT](state) {
            state.count -= 1;
          }
        }
      });
      const app = Vue.createApp({
        methods: {
          decrement() {
            this.$store.commit(DECREMENT);
          }
        },
        computed: {
          count() {
            return this.$store.state.count;
          }
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

We passed in a DECREMENT constant which we passed in as the name of the mutation in the mutations property and the commit method.

It’s useful for reusing the same name in multiple places.

Mutations Must Be Synchronous

Mutations must be synchronous.

This is because the code inside it must run in sequence so that we can trace the code.

Committing Mutations in Components

We can call the mapMutations method to map our mutations to a method.

For example, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="decrement">decrement</button>
      <p>{{count}}</p>
    </div>
    <script>
      const store = new Vuex.Store({
        state: {
          count: 0
        },
        mutations: {
          decrement(state) {
            state.count -= 1;
          }
        }
      });
      const app = Vue.createApp({
        methods: {
          ...Vuex.mapMutations(["decrement"])
        },
        computed: {
          count() {
            return this.$store.state.count;
          }
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

to call the Vuex.mapMutations method to a method.

Then we can run that when we click the decrement button.

Payloads are supported when we map mutations with mapMutations .

We can map a mutation to a method with a different name with:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
    <title>App</title>
  </head>
  <body>
    <div id="app">
      <button @click="subtract">decrement</button>
      <p>{{count}}</p>
    </div>
    <script>
      const store = new Vuex.Store({
        state: {
          count: 0
        },
        mutations: {
          decrement(state) {
            state.count -= 1;
          }
        }
      });
      const app = Vue.createApp({
        methods: {
          ...Vuex.mapMutations({
            subtract: "decrement"
          })
        },
        computed: {
          count() {
            return this.$store.state.count;
          }
        }
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>

We mapped the decrement mutation to the subtract method with the key of the object we passed into mapMutations .

Conclusion

We can create and mutations in various to change Vuex state data.