Created JS function to fetch static JSON. Now loads data Asynchronously :)

This commit is contained in:
Luke Else 2023-09-05 19:03:59 +01:00
parent 79795bc060
commit b2b56480b8
4 changed files with 27 additions and 7 deletions

10
rollup.config.js Normal file
View File

@ -0,0 +1,10 @@
import json from '@rollup/plugin-json';
export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [json()]
};

5
src/lib/data.ts Normal file
View File

@ -0,0 +1,5 @@
export async function getJson(path: string) {
let response = await fetch(path);
let users = await response.json();
return users;
}

View File

@ -1,5 +1,5 @@
<script>
import * as info from '../static/json/me.json';
import { getJson } from "$lib/data"
</script>
<style>
@ -50,10 +50,16 @@
</style>
<div class="card">
{#await getJson('/json/me.json')}
<h1>Loading...</h1>
{:then info}
<h1>{info.name}</h1>
<div class="flex-container">
<img class="profile" src={info.profile_photo} alt="{info.name}'s Profile Photo">
<p class="about">{info.about}</p>
</div>
{:catch error}
<h1>{error.message}</h1>
{/await}
</div>

View File

@ -1,5 +1,4 @@
<script>
import json from '@rollup/plugin-json'
</script>
<style>