Last active
April 10, 2024 20:01
-
-
Save helabenkhalfallah/0b1e9c20d82e69bff5bec1b9339cecc3 to your computer and use it in GitHub Desktop.
Using external Api
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
| <!-- Import the fetchData function from the api.js file --> | |
| <script> | |
| import { onMount } from 'svelte'; // Import the onMount function from Svelte | |
| import { fetchData } from './api'; // Import the fetchData function from the api.js file | |
| let responseData; // Declare a variable to store the fetched data | |
| // Make an HTTP GET request when the component mounts | |
| onMount(async () => { | |
| try { | |
| // Call the fetchData function to fetch data from the API | |
| responseData = await fetchData(); | |
| } catch (error) { | |
| // Handle the error if needed | |
| } | |
| }); | |
| </script> | |
| <!-- Conditional rendering based on the availability of responseData --> | |
| {#if responseData} | |
| <!-- Display the fetched data --> | |
| <p>{JSON.stringify(responseData)}</p> | |
| {:else} | |
| <!-- Show a loading message while data is being fetched --> | |
| <p>Loading...</p> | |
| {/if} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment