Skip to main content
  1. Posts/

How to save data to your API using Vuex

·603 words·3 mins
Joe Erickson
Author
Joe Erickson
Senior software developer specializing in web development, AI, and helping others learn to code.

I talked before about how to query your back-end API from Vuex, but that was mainly centered on getting data. What happens when you want to save data to a back-end API after it is updated or added in the Vuex store? What’s the best, most efficient way to accomplish this?

As I talked about in my article on the purpose of Vuex, Vuex is not meant to act as a central datastore. It’s not a permanent place to keep data by any means. If a user navigates away from the application or the user’s browser crashes, everything that was stored in Vuex will be gone. For this reason, we never want to store something in Vuex and tell the user it has changed if the real data (the data in the back-end database) has not actually changed. We want to make sure that data changes first before updating the UI and possibly misleading a user.

So, how can we accomplish this? Again, I have taken some inspiration from the Vue Real World project by gothinkster. I think that the way they handle it is pretty good. It assumes a couple of things:

  1. When data changes in the application, always send it to the back-end.
  2. Don’t save changed data in the Vuex store. Instead, get it again from the back-end.

Sending data to the back-end API using Actions
#

One of the requirements of using Vuex to work with a back-end API is that all changes to data should not go through mutations, but instead through actions. I talked at one point about why you might want to make logic changes to data in Vuex through actions and calling APIs certainly falls under the category “logic”.

So all data manipulation that needs to be saved to a back-end should happen through Actions. As an example, let’s imagine that our application allows adding a new user to a system that we are creating. When the form is submitted for a new user, what we want to do is have that passed to a Vuex action from the component:

methods: {
    submitUser() {
        this.$store.dispatch('saveUser', this.user);
    }
}

Then, in Vuex action, you can save that data by calling the back-end API:

actions: {
  saveUser(context, user) {
    Vue.axios.post('users', user);
  }
}

And then, once the promise resolves, make a call to load in the users again from the back-end:

actions: {
  saveUser(context, user) {
    Vue.axios.post('users', user).then(result => {
      context.dispatch('loadUsers');
    });
  },
  loadUsers({commit}) {
    ...
  }
}

So the flow here becomes:

  1. Call the action from the component to save the new user
  2. In the action, call the API to save the user data to the back-end
  3. After the back-end says everything is good, get the new data from the back-end

It might seem strange to get the data that we just saved to the back-end, but it solves a couple of pressing issues.

  1. The back-end should be in charge of final validation on all data. By getting the data from the back-end after saving it there, we ensure that the data is fully validated before we set that data in Vuex for the components to use.
  2. The back-end may need to change or format the data or set unique ids on the data. By passing the data to the back-end and then getting it back from the back-end, we can be sure the id and other data will be populated and that we’ll have it in the front-end.

One of our main goals should be to make sure our data stays in sync with the back-end. This is a good approach to make sure that happens.

Related

What's the purpose of Vuex?

·679 words·4 mins
There are a lot of things to worry about when it comes to Vue applications. You’ve got SPAs and routing and web workers and Jest and Cypress and everything else. It gets overwhelming pretty quickly. The hard part is not learning these technologies, it’s knowing what their place is in the application.

How to query your API using Vuex in your Vue application

·866 words·5 mins
Once you start using Vuex to manage the shared data of your Vue application, it becomes less clear on where or how to call your back-end API. I think everyone starts out making API calls in the created function of your component. But that doesn’t scale past a handful of components. Then, they end up loading in the same data over and over again and each component has it’s own copy, which is impossible to keep in sync. You lose the efficiency of having one part of the system in control of the data when you do that and on a larger application, this very quickly falls apart.

Should you always use getters in Vuex?

·1166 words·6 mins
One of the questions that comes up time and again concerning Vuex is “Do I always use a getter when accessing data? Or can I directly access the raw state?” It’s one of those things you hear about that you should do, but no one seems to really explain why. And do you really need to create a getter for every single piece of data that you put in the store? Isn’t that just a bunch of unneeded boilerplate and duplication?

Loading dynamic images in a Vue Component

·541 words·3 mins
When I first started using Vue, one thing I got continually wrong was how to load an image into a Vue component dynamically. At first, I found that using an absolute URL worked, but that was only useful if I was storing the images on a CDN or other external site. If I included the images in my project, as either lightweight icons or static images, then using an absolute URL, with hostname and all, didn’t really work. What about when I wanted to test some new images locally or on a dev server? Linking directly to the images in production just wasn’t going to cut it.