svelte note
- ๋ฅ์ง
- Development
- ๋ถ๋ฅ
- Javascript
- ๋ฑ๋ก์ผ
ํ๋ก ํธ์๋ ์๋ฒ๋ฅผ ์ด์ฉํ๊ธฐ์ํ ๋๊ตฌ
https://kit.svelte.dev/docs/adapters
ํธ์คํธ, ํฌํธ๋ฒํธ๊ฐ์ ์๋ฒํ๊ฒฝ์ ๋ํ ๋๊ตฌ์ ๊ดํ ๋ฌธ์๋ค.
๋
ธ๋์๋ฒ๋ฅผ ํ์ฅ์ํฌ ์ ์์ด ๋ณด์ธ๋ค. (๊ทธ๋ฅ sveltekit์์๋ ์์ค์์์ ์ฌ์ฉํ๋๊ฒ ๊ฐ์ ๋ณด์ด์ง๋ง..)
๋น๋๊ธฐ ๋ค์ด๋๋ฏน ์ปดํฌ๋ํธ ๊ตฌํํ๊ธฐ
๊ฐ๋จํ ํด์ ๋ผ์ฐํธ๋ฅผ ํตํ์ฌ ํญ์ ๋ง๋ค๊ณ , ์ฃผ์๋ฅผ ์ด๋ํ๋ฉด ์์ ์ปดํฌ๋ํธ๋ฅผ ๋ฐ๊ฟ์ฃผ๋ ๋ถ๋ถ์ด ํ์ํ์ฌ ๋ง๋ค๊ฒ ๋์๋ค.
์ด๋ํ๋ ์ฃผ์ ๋ชฉ๋ก์ ๋ค์๊ณผ ๊ฐ๋ค.
- /#/guide
- /#/guide/buttons
- /#/guide/form-elements
- /#/guide/icon
<!-- guide.svelte -->
<article>
<h1><a href="/#">HOME</a> / Guide page</h1>
<Tabs/>
{#await component}
<p>...waiting</p>
{:then comp}
<svelte:component this={comp}/>
{:catch err}
<p>Error page</p>
{/await}
</article>
<script lang="ts">
import Tabs from './tabs/index.svelte'
let hashArray: string[]
let component: any
const subPages = [
{ type: 'home', component: () => import('./home.svelte') },
{ type: 'icons', component: () => import('./icons.svelte') },
{ type: 'buttons', component: () => import('./buttons.svelte') },
{ type: 'form-elements', component: () => import('./form-elements.svelte') },
]
async function render(route: string)
{
const selectedRoute = subPages.find(o => o.type === route)
return (await selectedRoute.component() as any).default
}
hashArray = location.hash ? location.hash.replace(/^#\//, '').split('/') : [ 'guide', 'home' ]
component = render(hashArray[1] || 'home')
</script>
์ด๋ ๊ฒ ๊ธฐ์ด์ ์ธ ๊ตฌ์ฑ์ ๋๋ง ์์ฑํด๋๋ค. ์ถ์ฒ๋ ์ด๊ณณ์ ๊ณณ..
import๋ฅผ ํจ์ ํ์์ผ๋ก ์ฌ์ฉํ๊ธฐ
๋ค์ด๋๋ฏน ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ค์ ํ๋
<svelte:component this="{Chatbox}" />
<script>
let Chatbox;
function loadChatbox() {
import('./ChatBox.svelte').then(res => Chatbox = res.default)
}
</script>
์คํ์ผ์ํธ :global ์ฌ์ฉํ๊ธฐ
svelte ๊ธฐ๋ณธ์ scoped๋ฅผ ๊ธฐ๋ณธ์ผ๋ก ์ฌ์ฉํ๊ณ ์๋ค. ๊ธ๋ก๋ฒ๋ก ์ฌ์ฉํ๋ ค๋ฉด :global(body) {}๊ฐ์ ๋ชจ์ต์ผ๋ก ์ ํ์๋ฅผ ๋ง๋ค์ด์ผ ํ๋ค.
์์ฆ์ ์ผ๋ฐ css์์ nesting์ ์ฌ์ฉํ ๋ ๊ธ๋ก๋ฒ์์ ๊ฐ์ด ์ฌ์ฉํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
:global(:root) {
& :global(body) {
background: white;
}
}
:global(:root[data-theme=dark]) {
& :global(body) {
background: black;
}
}
@media (prefers-color-scheme: dark) {
:global(:root:not([data-theme=light])) {
& :global(body) {
background: black;
}
}
}
๋คํฌ๋ชจ๋์ ๊ดํ ์ ๋ ํฐ์ ๊ตฌ์กฐ๋ค.
ํน์ ๋ณ์๊ฐ ์ ๋ฐ์ดํธ๋ ๋๋ง๋ค ํจ์์คํํ๊ธฐ
$: c, console.log('yes')
์ด๋ฐ ๋ชจ์ต์ผ๋ก ์ฌ์ฉํ ์ ์๋ค. ๋ง์น vue์ watch๊ฐ์ด..
slot์ ๋น ์๋ฆฌ๋จผํธ๋ก ์ฌ์ฉํ๊ณ ์ถ์๋..
vue์์ <template #name><span>text</span><em>123</em></template> ์ ๊ฐ์ด ํน์ ์๋ฆฌ๋จผํธ๋ก ๊ฐ์ธ์ง ์๊ณ ๋ด์ฉ๋ฌผ๋ง ์ฌ์ฉํ๊ณ ์ถ์๋๊ฐ ๋ง๋ค.
์ด์ ๊น์ง ์ฐพ์๋ณด๊ณ ์๋๋์ค ์์๋๋ฐ ์ ์ฐพ์๋ณด๋ <svelte:fragment/>๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์์๋ค.
<Comp>
<h1 slot="header">Hello</h1>
<svelte:fragment slot="footer">
<p>text line</p>
<p>text line</p>
</svelte:fragment>
</Comp>
์ถ์ฒ: https://svelte.dev/docs/special-elements#svelte-fragment
1๊ฐ์ ๋๊ธ
web component ๋ก ๋ณํํ๋ ์ฃผ์ ์ ๋ํด ๋ค๋ฃจ์๋ค.
https://www.colorglare.com/svelte-components-as-web-components-b400d1253504