Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
title: Sharing code
title: 코드 공유하기
---

In all the examples we've seen so far, the `<script>` block contains code that runs when each component instance is initialised. For the vast majority of components, that's all you'll ever need.
지금까지 본 모든 예제에서 `<script>` 블록은 각 컴포넌트 인스턴스가 초기화될 때 실행되는 코드를 포함합니다. 대부분의 컴포넌트에 대해서는 이걸로 충분합니다.

Very occasionally, you'll need to run some code outside of an individual component instance. For example: returning to our custom audio player from a [previous exercise](media-elements), you can play all four tracks simultaneously. It would be better if playing one stopped all the others.
아주 가끔, 개별 컴포넌트 인스턴스 외부에서 코드를 실행해야 할 필요가 있습니다. 예를 들어, [이전 연습](media-elements)의 사용자 정의 오디오 플레이어로 돌아가서, 네 개의 트랙을 동시에 재생할 수 있습니다. 하나를 재생할 때 다른 모든 것을 중지하면 더 좋을 것입니다.

We can do that by declaring a `<script context="module">` block. Code contained inside it will run once, when the module first evaluates, rather than when a component is instantiated. Place this at the top of `AudioPlayer.svelte` (note that this is a _separate_ script tag):
이를 위해 `<script context="module">` 블록을 선언할 수 있습니다. 이 안에 포함된 코드는 컴포넌트가 인스턴스화될 때가 아니라, 모듈이 처음 평가될 때 한 번 실행됩니다. 이를 `AudioPlayer.svelte`의 맨 위에 배치합시다. (이는 _별도의_ 스크립트 태그입니다.)

```svelte
/// file: AudioPlayer.svelte
Expand All @@ -15,7 +15,7 @@ We can do that by declaring a `<script context="module">` block. Code contained
</script>+++
```

It's now possible for the components to 'talk' to each other without any state management:
이제 상태 관리를 하지 않고도 컴포넌트들이 서로 '대화'할 수 있습니다.

```svelte
/// file: AudioPlayer.svelte
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: Exports
title: 내보내기(exports)
---

Anything exported from a `context="module"` script block becomes an export from the module itself. Let's export a `stopAll` function:
`context="module"` 스크립트 블록에서 내보낸 모든 항목은 모듈 자체에서 내보내기가 됩니다. `stopAll` 함수를 내보내 봅시다.

```svelte
/// file: AudioPlayer.svelte
Expand All @@ -15,7 +15,7 @@ Anything exported from a `context="module"` script block becomes an export from
</script>
```

We can now import `stopAll` in `App.svelte`...
이제 `App.svelte`에서 `stopAll`을 가져올 수 있습니다...

```svelte
/// file: App.svelte
Expand All @@ -25,7 +25,7 @@ We can now import `stopAll` in `App.svelte`...
</script>
```

...and use it in an event handler:
...그리고 이벤트 핸들러에서 사용할 수 있습니다.

```svelte
/// file: App.svelte
Expand All @@ -40,4 +40,4 @@ We can now import `stopAll` in `App.svelte`...
</div>
```

> You can't have a default export, because the component _is_ the default export.
> 기본 내보내기(default export)를 가질 수는 없습니다. 컴포넌트 _자체가_ 기본 내보내기이기 때문입니다.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "Module context",
"title": "모듈 컨텍스트",
"scope": {
"prefix": "/src/lib/",
"name": "src"
Expand Down