Created
November 19, 2022 18:27
-
-
Save sbeugen/26d3c1b6b333d6a0c5d7ea7438ee3b75 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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