【问题标题】:Data API not appear using Vue JS (Rapidapi)使用 Vue JS (Rapidapi) 不会出现数据 API
【发布时间】:2021-07-14 09:16:09
【问题描述】:

我正在学习如何将 API 与 VueJS 一起使用。我在 Rapidapi 上获取了开源 API 数据。

我通过输入链接https://covid-19-data.p.rapidapi.com/report/country/name?name=Italy&date=2020-04-01 尝试使用 Postman,并得到了响应。

但是在VueJS中尝试时,数据没有出现。我的循环中是否有任何遗漏?

这是我的代码的托管代码框:https://codesandbox.io/s/nice-rain-45j6y

<template>
  <div v-for="post in posts" :key="post.id">
    {{ post.country }}
    {{ post.confirmed }}
  </div>
</template>

<script>
export default {
  data() {
     return {}
  },
  computed: {
    posts() {
        return this.$store.state.posts
    }
  },
  mounted() {
    this.$store.dispatch("loadPosts");
  }
};
</script>
import axios from 'axios'

const options = {
    method: 'GET',
    url: 'https://covid-19-data.p.rapidapi.com/country',
    params: {name: 'italy'},
    headers: {
      'x-rapidapi-key': '1031599022msh37c84da8c4e9b0ap1047d6jsn4d6475b64dc7',
      'x-rapidapi-host': 'covid-19-data.p.rapidapi.com'
    }
  };

const covid = {
    state: () => ({
      posts: []
    }),
    mutations:{
      setPosts(state, components){
         state.posts = posts
      }
    },
    actions: {
      loadPosts({ commit }) {
        axios.request(options)
        .then(response => {
            commit('setPosts', response.data)
            console.log(response.data)
        })
        .catch(function (error) {
            console.error(error);
        });
      }
    },

}

export default covid;

我的console.log(response.data) 得到的回复如下

【问题讨论】:

  • 当我在 Postman 上试用它时,我也收到了回复……这是一个带有消息 "message": "Invalid API key. Go to https://docs.rapidapi.com/docs/keys for more info." 的 401。我不明白你为什么对 axios 给你一个类似的错误感到惊讶。
  • 我应该把 api 密钥放在哪里? @昆汀
  • 我不知道。尝试阅读该服务的文档。
  • 您可能需要某种身份验证,因此是 401。
  • 我已经更改了上面的代码,并在控制台中得到了响应。但是为什么在vue js模板中没有出现@kissu

标签: javascript vue.js axios vuejs3 rapidapi


【解决方案1】:

看来您需要 API 密钥才能使用它。代码 401 表示未经授权,可能是因为您没有使用密钥。您可以获取您的 API 密钥并将其添加到查询参数中。您可以在此处阅读文档:

https://docs.rapidapi.com/docs/keys

https://example.p.rapidapi.com/?rapidapi-key=***************************

在你的情况下应该是这样的:

https://covid-19-data.p.rapidapi.com/report/country/name?name=Italy&date=2020-04-01&rapidapi-key=API_KEY_HERE

编辑 如果你想在组件中使用状态,你需要导入它们。

import { mapState } from 'vuex';

export default{

computed: { ...mapState(['posts']) }
}

您可以对操作执行相同的操作。 http://vuex.vuejs.org/guide/actions.html#dispatching-actions-in-components

【讨论】:

  • 我已经更改了上面的代码,并在控制台中得到了响应。但是为什么没有出现在vue js模板中
  • 显示错误?当您使用 vuex 时,您需要在组件中导入状态、操作……。我已经更新了我的回复
  • 请编辑您的答案,而不是在 cmets 中发布内联代码。他输入状态的方式很好。正如文档中所解释的:vuex.vuejs.org/guide/… 即使我也建议使用助手。 ^^
  • 我已经按照你上面写的那样改变了我的计算,但什么也没出现。我想问一下,计算出来的posts是从哪里来的?
  • OP 应该特别在此处安装适用于 VueJS v2 的开发工具:chrome.google.com/webstore/detail/vuejs-devtools/… 这样,他将能够检查其状态下的响应数组(vuex 选项卡)。
【解决方案2】:

感谢您的代码框,我实现了正确显示它

<template>
  <div>
    <div v-for="post in posts" :key="post.longitude"> <!-- be careful here, you had a longatitude typo here -->
      <div>country: {{ post.country }}</div>
      <div>code: {{ post.code }}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  },
  computed: {
    posts() {
      return this.$store.state.covid.posts
    },
  },
  mounted() {
    this.$store.dispatch('loadPosts')
  },
}
</script>

主要错误实际上在covid.js文件中

mutations: {
  setPosts(state, posts) { // was components here, but should be posts as the 2nd argument
    state.posts = posts;
  }
},

【讨论】:

  • posts() { return this.$store.state.covid.posts }, after state 必须是模块中的文件名吗??
  • 你在模块中有一个const covid = { state: () =&gt; ... 对象,所以你需要在你的例子中像这样访问它是的。同时,通常您只能将对象放在文件的根目录(不嵌套)并像export const yourFancyModule = { namespaced: true, state, getters, mutations, actions} 一样导出它们。然后将其导入到主存储文件中。
猜你喜欢
  • 1970-01-01
  • 2017-10-30
  • 2018-06-23
  • 1970-01-01
  • 2021-09-21
  • 2020-09-27
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
相关资源
最近更新 更多