Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1879

How do I import a text file in sveltekit?

$
0
0

I am trying to import a txt file and print the lines in my sveltekit app. I tried to fetch it with the server and also I tried to import it straight into the component but I cannot get either to work. I have tried adding the txt file to both the static folder and public. This is how I tried to import it on the component.

onMount(() => {        loadItems();    });    const loadItems = async () => {        const response = await fetch('public/safety.txt');        const text = await response.text();        items = text.split('\n\n'); // split on double newlines    };    let currentIndex = 0;    const interval = setInterval(() => {        currentIndex = Math.floor(Math.random() * items.length);    }, 5000);    $: currentItem = items[currentIndex];    onDestroy(() => {        clearInterval(interval);    });

And this is how I tried on the server:

const safety = await fetch('/public/safety.txt')

Viewing all articles
Browse latest Browse all 1879

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>