如何在nuxt.js中发送带有参数的post请求?

时间:2021-03-28 13:37:09

标签: api vue.js axios fetch nuxt.js

我正在使用 fetch 方法 nuxt/axios 发送 post 请求以获取特定类别

async fetch() {
    const res = await this.$axios.post(
      `https://example.com/art-admin/public/api/get_single_cat_data_and_posts?cat_id=${params.id}`,
      {
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json"
        }
      }
    );
    this.categoryData = JSON.parse(JSON.stringify(res.data.Data));
  },

但不工作是否有另一种方法可以通过带有参数的 post 请求获取数据

1 个答案:

答案 0 :(得分:2)

要访问有关您当前路线的信息,您应该调用 $route 对象。

如果您想访问在 URL 中传递的参数,那么您应该访问 $route.query

所以在你的情况下,你应该尝试打电话

`https://example.com/art-admin/public/api/get_single_cat_data_and_posts?cat_id=${this.$route.query.id}`,