将route.params.id传递给vuex操作

时间:2020-07-16 19:20:48

标签: vue.js vuex nuxt.js

请告诉我如何在单击特定类别时代替{cat_id},获取其ID并将其插入带有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 个答案:

答案 0 :(得分:1)

如果您想在操作中使用某些变量,请将其作为 payload 传递,例如

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

在你的行动中

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
}