【问题标题】:How to load an additional JSON data from another API in VUE?如何从 VUE 中的另一个 API 加载额外的 JSON 数据?
【发布时间】:2020-02-10 05:55:12
【问题描述】:

我正在使用 vue / axios 使用此 Pokeapi 设置 PokeDex 项目。目前我正在使用这个 JSON 文件加载所有口袋妖怪名称:https://pokeapi.co/api/v2/pokedex/1/

我想加载来自另一个 JSON 文件的额外口袋妖怪数据。例如:https://pokeapi.co/api/v2/pokemon/bulbasaur/

除了当前的设置之外,我如何动态加载这些数据?

当前设置:

<template>

<div id="app">

    <div class="pokemon" v-for="pokemon in info">

        <span >{{ pokemon.entry_number }}</span>
        <span >{{ pokemon.pokemon_species.name }}</span>        

    </div>

</div>

</template>

<script>

export default {

name: '#app',

data () {
return {
    info: [],
    loading: true,
    errored: false,
}
},

methods: {

},

mounted () {

    axios.get('https://pokeapi.co/api/v2/pokedex/1/')

    .then(response => {
        this.info = response.data.pokemon_entries
    })

    .catch(error => {
        console.log(error)
        this.errored = true
    })

    .finally(() => this.loading = false)

}

};
</script>

【问题讨论】:

  • 是否有第二个 API url 是静态的,还是该 url 取决于第一次调用的结果?
  • 嗨@SimonThiel,取决于第一次调用:第一个 API:pokeapi.co/api/v2/pokedex/1 第二个 API pokeapi.co/api/v2/pokemon + 'name of pokemon/' 所以我猜 'name of the pokemon' 应该填充为第一次调用的结果。
  • 当您可以在第一个 API 的“then”中简单地调用它时
  • 还是第一个 API 返回一个列表,您需要检索所有条目的详细信息?
  • 是的,@SimonThiel,我需要检索所有条目的其他详细信息。第一个 API 只返回名称 + url。

标签: json api vue.js


【解决方案1】:

我花时间研究了您的 API 并实现了您希望的代码。

只要info 数组被第一个 API 调用的数据填充,我就使用观察程序调用第二个 API(有关详细信息,请参阅:https://vuejs.org/v2/guide/computed.html#Watchers)。此外,我将调用分成不同的方法来改进代码:

 <template>
  <div id="app">
    <div class="pokemon" v-for="(pokemon, index) in info" :key="index">
      <span>{{ pokemon.entry_number }}</span>
      <span>{{ pokemon.pokemon_species.name }}</span>
      <span v-if="pokemon.details">-- details: base_experience{{ pokemon.details.base_experience }}</span>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "#app",

  data() {
    return {
      info: [],
      loading: true,
      errored: false
    };
  },
  watch: {
    info(newValue, oldValue) {
      if (oldValue.length === 0 && newValue.length > 0) {
        this.loadDetails();
      }
    }
  },
  methods: {
    getInitialData() {
      axios
        .get("https://pokeapi.co/api/v2/pokedex/1/")

        .then(response => {
          this.info = response.data.pokemon_entries;
        })

        .catch(error => {
          console.log(error);
          this.errored = true;
        })

        .finally(() => (this.loading = false));
    },

    loadDetails() {
      this.info.forEach((entry, index) => {
        const pokemonName = entry.pokemon_species.name;
        const secondApiUrl = `https://pokeapi.co/api/v2/pokemon/${pokemonName}/`;
        this.getDetailDataByPokemon(secondApiUrl, pokemonName);
      });
    },
    getDetailDataByPokemon(secondApiUrl, pokemonName) {
      axios.get(secondApiUrl).then(response => {
        this.mapDetailsToInfo(response, pokemonName);
      });
    },
    mapDetailsToInfo(response, pokemonName) {
      // here you can map the required infos to your initial info array.
      // as an example I mapped the base_experience
      const relevantDetail = response.data.base_experience;
      this.info = this.info.map(entry => {
        const mappedEntry = entry;
        if (entry.pokemon_species.name === pokemonName) {
          // here you can map any value
          mappedEntry.details = { base_experience: relevantDetail };
        }
        return mappedEntry;
      });
    }
  },

  mounted() {
    this.getInitialData();
  }
};
</script>

【讨论】:

  • 哇,西蒙,这太棒了。非常感谢您花时间解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-02
  • 1970-01-01
  • 2017-12-06
  • 2017-05-24
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
相关资源
最近更新 更多