Svelte Basic

Published at Jan 23, 2024

#sveltekit #svelte

svelte-basic

Svelte Learn!

์ƒํƒœ๊ฐ’

<script>
	let count = 0;
	//์ƒํƒœ๋Š” ๋ณ€์ˆ˜ ๋ฟ์•„๋‹ˆ๋ผ ๋ฐฐ์—ด, object, object array๋“ฑ js์— ๋Œ€๋ถ€๋ถ„์— ํƒ€์ž…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ
	function handleClick() {
		count += 1;
	}
</script>

<button on:click={handleClick}>
	ํด๋ฆญ์ˆ˜ { count }
</button>

๋ฐ˜์‘์„ฑ

<script>
	let count = 0
	$: doubled = count * 2

	$: if(count >= 10) {
		alert('์นด์šดํŠธ 10์„ ๋„˜์—ˆ์Šต๋‹ˆ๋‹ค.')
		count = 9
	}
	//if๋ฌธ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
	$: {
		console.log( count )
		console.log( doubled )
	}
	//๋ฐ˜์‘์„ฑ์€ $๋กœ ํ‘œ์‹œํ•˜๊ณ  ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
	function handleClick() {
		count += 1;
	}
</script>

<button on:click={handleClick}>
	ํด๋ฆญ์ˆ˜ { count } { count === 1 ? 'time' : 'times'}
</button>

<p>{count} ๋‘๋ฐฐ๋Š” {doubled}</p>

์ƒํƒœ๊ฐ’ ๋ณ€๊ฒฝ์„ ๋™์‹œ์ ์œผ๋กœ ๋น ๋ฅด๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Œ

Component

<script>
	import Header from './header.svelte'
	import Content from './content.svelte'
	import Footer from './footer.svelte'
</script>

<Header />
<Content />
<Content />
<Content />
<Footer/>

์ปดํฌ๋„ŒํŠธ๋ฅผ importํ•ด์˜ฌ๋•Œ๋Š” ๋Œ€๋ฌธ์ž ์‹œ์ž‘ํ•ด์•ผํ•˜๊ณ  html๋‹จ์—์„œ ๊ทธ๋ƒฅ ํƒœ๊ทธ ์“ฐ๋Š”๊ฒƒ์ฒ˜๋Ÿผ ์“ฐ๋ฉด ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์‘

props

//app.svelte
<script>
	import CountComp from './countComp.svelte'
	import BtnComp from './btnComp.svelte'
	let count = 0
	$: doubled = count * 2

	$: if(count >= 10) {
		alert('์นด์šดํŠธ 10์„ ๋„˜์—ˆ์Šต๋‹ˆ๋‹ค.')
		count = 9
	}

	function handleClick() {
		count += 1;
	}
</script>

<BtnComp { count } { handleClick }/>

<CountComp { count } { doubled }/>
<script>
    //btnComp.svelte
	export let count;
	export let handleClick;

	import LabelComp from './labelComp.svelte'
</script>

<button on:click={handleClick}>
	ํด๋ฆญ์ˆ˜ { count } <LabelComp {count}/>
</button>

props๋Š” ๋ถ€๋ชจ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์‹์ปดํฌ๋„ŒํŠธ์—๊ฒŒ๋งŒ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Œ ํ•จ์ˆ˜๋˜ ๋ณ€์ˆ˜๋˜ export let ๋ณ€์ˆ˜๋ช…ํ•˜๋ฉด ์ž์‹์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

๋‹จ๋ฐฉํ–ฅ ๋ฐ”์ธ๋”ฉ์ž„ ๊ทธ๋ž˜์„œ context, dipatcher, store๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ๋ผ๋ฆฌ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ์Œ

๋…ผ๋ฆฌ๋ธ”๋ก

<script>
	let auth = {
		loggedIn: false
	}

	const handleLogin = () => auth.loggedIn = true;
	const handleLogout = () => auth.loggedIn = false;
	
</script>

<button on:click={handleLogin}>Log In </button>
<button on:click={handleLogout}>Log out</button>

{#if auth.loggedIn === true}
	<p>๋กœ๊ทธ์ธ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.</p>
{:else}
 <p>๋กœ๊ทธ์•„์›ƒ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.</p>
{/if}    

์ €๋ ‡๊ฒŒ #if๋ฅผ ์น˜๋ฉด ์กฐ๊ฑด๋ฌธ์„ ์“ธ ์ˆ˜ ์žˆ์Œ ๋งˆ์ง€๋ง‰์—” /if๋กœ ๋‹ซ์•„์ค˜์•ผํ•จ

๋ฐ˜๋ณต๋ธ”๋ก

<script>
	let todos = [
		{
			id: 0,
			content: '์ฒซ ๋ฒˆ์งธ ํ• ์ผ',
			done: false
		},
		{
			id: 1,
			content: '๋‘ ๋ฒˆ์งธ ํ• ์ผ',
			done: false
		},
		{
			id: 2,
			content: '์„ธ ๋ฒˆ์งธ ํ• ์ผ',
			done: false
		},
		{
			id: 3,
			content: '๋„ค ๋ฒˆ์งธ ํ• ์ผ',
			done: false
		}
	]
</script>

<ul>
	{#each todos as todo}
		<li>
			<span>
				{todo.id}
			</span>
			<span>
				{todo.content}
			</span>
			<span>
				{todo.done}
			</span>
		</li>
	{/each}
</ul>

๋ฐฐ์—ด์— ๋‚ด์šฉ์„ #each๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์—ฐ์†์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

store

์ „์—ญ์œผ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ์ƒํƒœ๊ฐ’ ์ €์žฅ์†Œ

import { writable } from 'svelte/store';


function createCount() {
    const { subscribe, set, update } = writable(0);
    
    const increment = () => update(count => count +1)
    //update๋Š” ๊ฐ’์„ ๋ฐ›์•„์™€ ์—ฐ์‚ฐํ•˜๊ณ , ๊ฐ’์„ ๋‹ค์‹œ ๋ฆฌํ„ดํ•ด์คŒ
    const decrement = () => update(count => count -1)
    const reset = () => set(0)
    //set์€ ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•ด์คŒ
    return {
        subscribe,
        increment,
        decrement,
        return
    }
    //์•ˆ์— ๋‚ด์šฉ์€ ๋ฌด์กฐ๊ฑด ๋ฆฌํ„ดํ•ด์ค˜์•ผํ•จ

    export cosnt count = createCount();
}
//์‚ฌ์šฉ์ž ์ง€์ • ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ•์€ ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ๋˜‘๊ฐ™์Œ

//writable์„ importํ•ด์˜จ ํ›„ ์ €๋ ‡๊ฒŒ export๋กœ ์„ ์–ธํ•˜๋ฉด ์ƒํƒœ๊ฐ’ ์ €์žฅ์†Œ๊ฐ€ ์ƒ๊ธฐ๊ณ  ์ €๊ฑธ ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ• ๋•Œ๋Š” ์ปดํฌ๋„ŒํŠธ importํ•ด์˜ค๋Š”๊ฑฐ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด ๋จ

slot

์ปดํฌ๋„ŒํŠธ ์žฌ์‚ฌ์šฉํ• ๋•Œ ์‚ฌ์šฉํ•จ

card.svelte

<article class="contact-card">
	<h2>
		<slot name="name">
			<span class="missing">๋ฏธ์ž…๋ ฅ</span>
		</slot>
	</h2>

	<div class="address">
		<slot name="address">
			<span class="missing">๋ฏธ์ž…๋ ฅ</span>
            //Missing์„ ์“ฐ๋ฉด ์ž…๋ ฅ๋˜์ง€ ์•Š์•˜์„๋•Œ ๋ฏธ์ž…๋ ฅ์ด๋ผ๊ณ  ์ถœ๋ ฅ๊ฐ€๋Šฅํ•จ
		</slot>
	</div>

	{#if $$slots.email}
    //'$$slots.์Šฌ๋กฏ์ด๋ฆ„'์„ ์ ์œผ๋ฉด ์Šฌ๋กฏ์ด ์‚ฌ์šฉ๋˜์—ˆ๋Š”์ง€ ์•ˆ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ
		<div class="email">
			<hr>
			<slot name="email"></slot>
		</div>
	{/if}
</article>
<script>
	import Card from './card.svelte'
</script>

<Card>
	<span slot="name">
		ํ™๊ธธ๋™
	</span>

	<span slot="address">
		tjdnfxmrquftl<br>
		์—ฌ์˜๋„๋™
	</span>					

</Card>
<Card>
	<span slot="name">
		ํ™๊ธธ๋™
	</span>
    //slotํด๋ž˜์Šค๋งˆ๋‹ค ๋‹ค๋ฅด๊ฒŒ ๋„ฃ์–ด์„œ ์ด๋ฆ„๊ณผ ํƒœ๊ทธ๋ฅผ ๋ฐ”๊ฟ”์“ธ์ˆ˜ ์žˆ์Œ

	<span slot="address">
		tjdnfxmrquftl<br>
		์—ฌ์˜๋„๋™
	</span>					

	<span slot="email">
		a243qw@masdgas.com
	</span>	
</Card>

div๋ง๊ณ  svelte:fragment๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด div๋Œ€์šฉ์œผ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅ

3boku ยฉ 2025