43

I'm currently working on a new Vue.js application. It depends heavily on api calls to my backend database.

For a lot of things I use Vuex stores because it manages shared data between my components. When looking at other Vue projects on github I see a special vuex directory with files that handles all the actions, states and so on. So when a component has to call the API, it includes the actions file from the vuex directory.

But, for messages for example, I don't want to use Vuex because those data is only important for one specific view. I want to use the component specific data here. But here is my problem: I still need to query my api. But I shouldn't include the Vuex actions file. So in that way I should create a new actions file. This way I have a specific file with api actions for vuex and for single components.

How should I structure this? Creating a new directory 'api' that handles actions for both vuex data and component-specific data? Or separate it?

2
  • Simply put the call inside the component and not store the result in vuex since it's only for this component? Commented Nov 26, 2016 at 4:32
  • 1
    Yes but how to structure my files? Should I create two different actions files? One for vuex, one for component specific? Or merge them? Commented Nov 26, 2016 at 9:23

3 Answers 3

35

I am using axios as HTTP client for making api calls, I have created a gateways folder in my src folder and I have put files for each backend, creating axios instances, like following

myApi.js

import axios from 'axios'
export default axios.create({
  baseURL: 'http://localhost:3000/api/v1',
  timeout: 5000,
  headers: {
    'X-Auth-Token': 'f2b6637ddf355a476918940289c0be016a4fe99e3b69c83d',
    'Content-Type': 'application/json'
  }
})

Now in your component, You can have a function which will fetch data from the api like following:

methods: {
 getProducts () {
     myApi.get('products?id=' + prodId).then(response =>  this.product = response.data)
  }
}

Similarly you can use this to get data for your vuex store as well.

Edited

If you are maintaining product related data in a dedicate vuex module, you can dispatch an action from the method in component, which will internally call the backend API and populate data in the store, code will look something like following:

Code in component:

methods: {
 getProducts (prodId) {
     this.$store.dispatch('FETCH_PRODUCTS', prodId)
  }
}

Code in vuex store:

import myApi from '../../gateways/my-api'
const state = {
  products: []
}

const actions = {
  FETCH_PRODUCTS: (state, prodId) => {
    myApi.get('products?id=' + prodId).then(response => state.commit('SET_PRODUCTS', response))
  }
} 

// mutations
const mutations = {
  SET_PRODUCTS: (state, data) => {
    state.products = Object.assign({}, response.data)
  }
}

const getters = {
}

export default {
  state,
  mutations,
  actions,
  getters
}
Sign up to request clarification or add additional context in comments.

7 Comments

And where do you save the actions regarding Vuex? For example to add new data into Vuex? Do you have an actions.js in the Vuex folder. Or do you do that inside each component?
Vuex related code is separate, you are choose how to arrange it, one way can be like this in cart example, I don't have dedicated actions file, I have different vuex module with their actions, mutations etc in one file.
So inside getProducts() in your component you call an action to update the Vuex store?
@Jordy Yes, If I am maintaining data in productStore than I can trigger a mutation here, or I can dispatch an action from getProducts() which will call backend API and trigger the mutation.
@Saurabh Thank you for the answer! For how long do you keep stale data in the Vuex store? When would you decide to retrieve current data from a server?
|
6

Note: vue-resource is retired ! Use something else, such as Axios.

I'm using mostly Vue Resource.I create services directory, and there put all connections to endpoints, for e.g PostService.js

import Vue from 'vue'

export default {
  get(id) {
    return Vue.http.get(`/api/post/${id}`)
  },
  create() {
    return Vue.http.post('/api/posts') 
  }
  // etc
}

Then in my file I'm importing that service and create method that would call method from service file

SomeView.vue

import PostService from '../services/PostService'

export default {
  data() {
    item: []
  },
  created() {
    this.fetchItem()
  },
  methods: {
    fetchItem() {
      return PostService.get(to.params.id)
        .then(result => {
          this.item = result.json()
        })
    }  
  }
}

3 Comments

Great, thank you. And where do you store all actions related to Vuex? Like where you store information about the user?
@Jordy sorry I don't use Vuex in my projects yet.
@Jordy I store all my actions, getters, mutations, plugin and state inside of a store folder located inside of /src
5

Based on concept of Belmin Bedak`s answer, i have wrapped it all into a simple library:

https://github.com/robsontenorio/vue-api-query

You can request your API like this:

All results

// GET /posts?filter[status]=ACTIVE

let post = await Post
  .where('status', 'ACTIVE')
  .get()

Specific result

// GET /posts/1

let post = await Post.find(1)

Editing

// PUT /posts/1 

post.title = 'Awsome!'
post.save()

Relationships

// GET /users/1
let user = await User.find(1)

// GET users/1/posts
let posts = await user
  .posts()
  .get()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.