First time here? Checkout the FAQ!
x
menu search
brightness_auto
more_vert

How to show html data from json api in svelte app ?

thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike

1 Answer

more_vert
 
verified
Best answer

In order to show data first of all you have to fetch it

if it is in same directory or inside the app

you can simply import it

<script>
   import data from './data.json';
</script>

or to fetch data you can

<script context="module">
    export async function load({fetch, params}) {
        let project;
        try {
            project = await fetch(`https://ajinkgupta.me/ajinkgupta/project/${params.slug}.json`);
            project = await project.json();
        } catch (e) {
            console.log(e);
        }
        return {
            props: {
                project
            }
        };
    }
</script>


<script>
    export let project;
</script>

to display data you can use this syntax

{project.name}

Svelte

thumb_up_off_alt 1 like thumb_down_off_alt 0 dislike
...