pass route.params.id to vuex actions

Viewed 1134

please tell me how instead of {cat_id} when clicking on a specific category, take its id and insert it into the link with the request in vuex

<nuxt-link :to="`/products/${category.id}`" class="menu-button">{{ category.title }}</nuxt-link>
export const actions = {
  GET_PRODUCTS({commit}) {
    return axios('https://example.com/api/get-items.php?cat={cat_id}&token=0e94e098eac6e56a22496613b32',{
      method: "GET"
    })
      .then((products) => {
        commit('SET_PRODUCTS', products.data);
        return products;
      })
      .catch((error) => {
        console.log("error")
        return error;
      })
  },
};
1 Answers

If you want to use some variable in your action, pass it in as the payload, eg

this.$store.dispatch('GET_PRODUCTS', { cat: this.$route.params.id })

And in your action

async GET_PRODUCTS({ commit }, { cat }) {
  const products = await axios("https://example.com/api/get-items.php", {
    method: "GET",
    params: {
      cat,
      token: "0e94e098eac6e56a22496613b32"
    }
  })
  commit('SET_PRODUCTS', products.data)
  return products
}
Related