Skip to content

Instantly share code, notes, and snippets.

@sbeugen
Created November 19, 2022 18:27
Show Gist options
  • Select an option

  • Save sbeugen/26d3c1b6b333d6a0c5d7ea7438ee3b75 to your computer and use it in GitHub Desktop.

Select an option

Save sbeugen/26d3c1b6b333d6a0c5d7ea7438ee3b75 to your computer and use it in GitHub Desktop.
<script setup>
const currentId = ref(1);
const currentPokemon = ref();
const fetchPokemonForCurrentId = async () => {
const pokemon = await $fetch(`https://pokeapi.co/api/v2/pokemon/${currentId.value}`);
currentPokemon.value = {
name: pokemon.name,
imageUrl: pokemon.sprites.front_default,
};
};
onBeforeMount(() => {
fetchPokemonForCurrentId();
});
const nextPokemon = async () => {
currentId.value++;
await fetchPokemonForCurrentId();
};
const previousPokemon = async () => {
if (currentId.value > 1) {
currentId.value--;
}
await fetchPokemonForCurrentId();
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment