DEV/svelte

svelte 데이터 불러오기, 불러온 데이터 사용하기

석봉 2023. 11. 28. 15:49

데이터를 불러올 route에 "+page.js" 파일을 생성하고 다음과 같이 데이터를 export한다.

 

본인은 src/routes/sample 에서 테스트하였다.

 

데이터를 불러온 후 reuturn할 export 작성
해당 라우트에서 data 가져오고 출력

 

정상적으로 출력됨을 알 수 있음.

 

 

svelte는 SSR(Sever Side Rendering) 또한 지원하기때문에 클라이언트에 노출되지 않고 사용하고 싶다면

"+page.server.js"로 파일을 생성하면 된다.

 

DB접근이나 API키 사용 등 필요에 따라 사용하면 된다.

공식 문서 참고

 

ref)

https://kit.svelte.dev/docs/routing

 

Routing • Docs • SvelteKit

Routing Edit this page on GitHub On this page On this page At the heart of SvelteKit is a filesystem-based router. The routes of your app — i.e. the URL paths that users can access — are defined by the directories in your codebase: src/routes is the ro

kit.svelte.dev