https://youtu.be/uQdkYDW5yJo?si=rKTJ285WisiN5wpi
$ npm create svelte@latest my-app
$ cd my-app
$ npm install
$ npm run dev
상호작용
Ok to proceed? (y)
: yWhere should we create your project?
: EnterWhich Svelte app template?
: Skeleton projectAdd type checking with TypeScript?
: NoSelect additional options (use arrow keys/space bar)
Add ESLint for code linting
Add Prettier for code formatting
picocss 프레임워크 활용 https://picocss.com/docs/
src > app.html
에 붙여 넣기
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css">
src > routes > +page.svelte
는 SvelteKit 프레임워크에서 정한 메인페이지
File-based Routing
PATH | Svelte | HTML |
---|---|---|
도메인/ |
src/routes/+page.svelte |
index.html |
도메인/login |
src/routes/login/+page.svelte |
login.html |
도메인/about |
src/routes/about/+page.svelte |
about.html |
+page.js
export const load = async ({ params }) {
// fetch post from api
return { post }
}
+page.svelte
<script>
export let data
</script>
<h1>{data.post.title}</h1>
<p>{data.post.body}</p>
src/routes/+page.js
구현
export const load = async () => {
const posts = await fetch('https://jsonplaceholder.typicode.com/posts');
return { posts: await posts.json() };
}
+page.server.js
export const load = async ({params}) {
// fetch post form database
return { post }
}
+page.svelte
<script>
export let data;
</script>
<h1>{data.post.title}</h1>
<p>{data.post.body}</p>
+page.svelte
<form action="?/createPost" method="POST">
<input name="title" />
<textarea name="body" />
<button type="submit">Create Post</button>
</form>
+page.server.js
export const actions = {
createPost: async ({ request }) => {
const body = await request.formData()
const title = body.get("title")
const content = body.get("body")
// handle post creation
}
// updatePost, deletePost, etc
}
/logout/+server.js
export const POST = ({ request }) => {
// Logout logic
}
/+logout.svelte
<nav>
<a href="/">홈</a>
<a href="/about">어바웃</a>
<a href="/profile">프로필</a>
</nav>
<slot />
<footer>
<p>Contact info</p>
</footer>
/+page.svelte
<h1>코딩셀러 홈페이지입니다</h1>
/about/+page.svelte
<h1>알아서 뭐하게?</h1>
/profile/+page.svelte
<h1>등산과 독서를 좋아함</h1>
+layout.server.js
export const load = () => {
// Get current user
return { user }
}
+layout.svelte
<script>
export let data;
</script>
<nav>Logged-in as {data.user.email}</nav>
+page.svelte
<script>
export let data;
</script>
<nav>Welcome {data.user.name}</nav>
$lib
으로 쉽게 import
할 수 있음(../../../
<- 상대경로 지옥에서 탈피)버튼 컴포넌트 경로: lib/components/Button.svelte
<button>
<slot />
</button>
프로젝트 폴더 내의 특정 페이지 파일 (../../../+page.svelte
)
<script>
import Button from "$lib/components/Button.svelte"
</script>
<Button>커스터마이즈 Button</Button>
경로: lib/server/db.js
// db 클라이언트 인스턴스 생성
export default db
/posts/+page.server.js
import db from "$lib/server/db"
export const load = () => {
return { posts: db.posts.findMany() }
}