Skip to content

Commit 3b2c0b7

Browse files
authored
Merge pull request #246 from niceplugin/api/composition-api-setup
문서 번역: api/composition-api-setup.md
2 parents afbd974 + 9e90944 commit 3b2c0b7

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed
Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
:::warning 현재 이 문서는 번역 작업이 진행중입니다
2-
:::
3-
4-
# Composition API: setup()
1+
# 컴포지션 API: setup() {#composition-api-setup}
52

63
:::info Note
7-
This page documents the usage of the `setup` component option. If you are using Composition API with Single-File Components, [`<script setup>`](/api/sfc-script-setup.html) is recommended for a more succinct and ergonomic syntax.
4+
이 페이지는 컴포넌트의 `setup` 옵션 사용법을 설명합니다.
5+
싱글 파일 컴포넌트에 컴포지션 API를 사용하는 경우,
6+
보다 간결하고 인체공학적인 문법을 위해 [`<script setup>`](/api/sfc-script-setup.html) 사용을 권장합니다.
87
:::
98

10-
The `setup()` hook serves as the entry point for Composition API usage in components in the following cases:
9+
`setup()` 훅은 다음과 같은 경우, 컴포넌트에서 컴포지션 API 사용을 위한 진입점 역할을 합니다:
1110

12-
1. Using Composition API without a build step;
13-
2. Integrating with Composition-API-based code in an Options API component.
11+
1. 빌드 과정 없이 컴포지션 API 사용.
12+
2. 옵션 API 컴포넌트에서 컴포지션 API 기반 코드와 통합.
1413

15-
## Basic Usage
14+
## 기본 사용법 {#basic-usage}
1615

17-
We can declare reactive state using [Reactivity APIs](./reactivity-core.html) and expose them to the template by returning an object from `setup()`. The properties on the returned object will also be made available on the component instance (if other options are used):
16+
[반응형 API](./reactivity-core.html)를 사용하여 반응형 상태를 선언하고 `setup()`에서 객체를 반환하여 템플릿에 노출할 수 있습니다.
17+
반환된 객체의 속성은 컴포넌트 인스턴스에서 사용할 수 있습니다(옵션 API가 사용되는 경우):
1818

1919
```vue
2020
<script>
@@ -24,7 +24,7 @@ export default {
2424
setup() {
2525
const count = ref(0)
2626
27-
// expose to template and other options API hooks
27+
// 템플릿 및 기타 옵션 API 훅에 노출
2828
return {
2929
count
3030
}
@@ -41,15 +41,20 @@ export default {
4141
</template>
4242
```
4343

44-
Note that [refs](/api/reactivity-core.html#ref) returned from `setup` are [automatically shallow unwrapped](/guide/essentials/reactivity-fundamentals.html#deep-reactivity) when accessed in the template so you do not need to use `.value` when accessing them. They are also unwrapped in the same way when accessed on `this`.
44+
`setup`에서 반환된 [refs](/api/reactivity-core.html#ref)는 템플릿에서 접근할 때,
45+
[자동으로 얕은 언래핑](/guide/essentials/reactivity-fundamentals.html#deep-reactivity)되므로, 접근할 때 `.value`를 사용할 필요가 없습니다.
46+
또한 `this`에서 접근할 때, 같은 방식으로 언래핑 됩니다.
4547

4648
:::tip
47-
`setup()` itself does not have access to the component instance - `this` will have a value of `undefined` inside `setup()`. You can access Composition-API-exposed values from Options API, but not the other way around.
49+
`setup()` 자체에는 컴포넌트 인스턴스에 대한 접근 권한이 없습니다.
50+
`setup()` 내부에서 `this`의 값은 `undefined`입니다.
51+
Composition API에서 노출한 값은 Options API에서 접근할 수 있지만, 그 반대는 불가능합니다.
4852
:::
4953

50-
## Accessing Props
54+
## Props에 접근하기 {#accessing-props}
5155

52-
The first argument in the `setup` function is the `props` argument. Just as you would expect in a standard component, `props` inside of a `setup` function are reactive and will be updated when new props are passed in.
56+
`setup` 함수의 첫 번째 인자는 `props`입니다.
57+
`setup` 함수 내부의 `props`는 반응형이며, 새 props가 전달되면 업데이트됩니다.
5358

5459
```js
5560
export default {
@@ -62,49 +67,52 @@ export default {
6267
}
6368
```
6469

65-
Note that if you destructure the `props` object, the destructured variables will lose reactivity. It is therefore recommended to always access props in the form of `props.xxx`.
70+
`props` 객체를 분해할 경우, 분해 된 변수는 반응성을 잃게 됩니다.
71+
따라서 항상 `props.xxx`처럼 접근하는 것이 좋습니다.
6672

67-
If you really need to destructure the props, or need to pass a prop into an external function while retaining reactivity, you can do so with the [toRefs()](./reactivity-utilities.html#torefs) and [toRef()](/api/reactivity-utilities.html#toref) utility APIs:
73+
Props를 분해해야 하거나, 반응성을 유지하면서 외부 함수에 props를 전달해야 하는 경우,
74+
[toRefs()](./reactivity-utilities.html#torefs) 또는 [toRef()](/api/reactivity-utilities.html#toref) 유틸리티 API를 사용하여 구현할 수 있습니다.
6875

6976
```js
7077
import { toRefs, toRef } from 'vue'
7178

7279
export default {
7380
setup(props) {
74-
// turn `props` into an object of refs, then destructure
81+
// refs 객체로 `props`를 변환한 후, 분해 할당
7582
const { title } = toRefs(props)
76-
// `title` is a ref that tracks `props.title`
83+
// `title``props.title`을 추적하는 ref 입니다.
7784
console.log(title.value)
7885

79-
// OR, turn a single property on `props` into a ref
86+
// 또는, 하나의 `props` 속성만 ref로 변환할 수 있습니다.
8087
const title = toRef(props, 'title')
8188
}
8289
}
8390
```
8491

8592
## Setup Context
8693

87-
The second argument passed to the `setup` function is a **Setup Context** object. The context object exposes other values that may be useful inside `setup`:
94+
`setup` 함수에 전달되는 두 번째 인자는 **Setup Context** 객체입니다.
95+
컨텍스트 객체는 `setup` 내부에서 유용할 수 있는 다른 값을 노출합니다:
8896

8997
```js
9098
export default {
9199
setup(props, context) {
92-
// Attributes (Non-reactive object, equivalent to $attrs)
100+
// 속성 (비-반응형 객체, $attrs에 해당함)
93101
console.log(context.attrs)
94102

95-
// Slots (Non-reactive object, equivalent to $slots)
103+
// 슬롯 (비-반응형 객체, $slots에 해당함)
96104
console.log(context.slots)
97105

98-
// Emit events (Function, equivalent to $emit)
106+
// 이벤트 발송 (함수, $emit에 해당함)
99107
console.log(context.emit)
100108

101-
// Expose public properties (Function)
109+
// 로컬 속성 노출 (함수)
102110
console.log(context.expose)
103111
}
104112
}
105113
```
106114

107-
The context object is not reactive and can be safely destructured:
115+
컨텍스트 객체는 반응형이 아니며, 안전하게 분해 할당될 수 있습니다:
108116

109117
```js
110118
export default {
@@ -114,31 +122,37 @@ export default {
114122
}
115123
```
116124

117-
`attrs` and `slots` are stateful objects that are always updated when the component itself is updated. This means you should avoid destructuring them and always reference properties as `attrs.x` or `slots.x`. Also note that, unlike `props`, the properties of `attrs` and `slots` are **not** reactive. If you intend to apply side effects based on changes to `attrs` or `slots`, you should do so inside an `onBeforeUpdate` lifecycle hook.
125+
`attrs``slots`는 컴포넌트가 업데이트될 때, 항상 업데이트되는 스테이트풀(stateful) 객체입니다.
126+
즉, 구조를 분해하지 말고 `attrs.x``slots.x`와 같이 속성을 참조해야 합니다.
127+
또한 `props`와 달리 `attrs``slots`의 속성은 **반응형이 아닙니다**.
128+
따라서 `attrs` 또는 `slots`의 변경 사항을 기반으로 하는 사이드 이펙트는 `onBeforeUpdate` 수명 주기 훅 내에서 구현해야 합니다.
118129

119-
### Exposing Public Properties
130+
### 퍼블릭 속성 노출하기 {#exposing-public-properties}
120131

121-
`expose` is a function that can be used to explicitly limit the properties exposed when the component instance is accessed by a parent component via [template refs](/guide/essentials/template-refs.html#ref-on-component):
132+
`expose` 함수는 부모 컴포넌트가 [템플릿 참조](/guide/essentials/template-refs.html#ref-on-component)를 통해 자식 컴포넌트 인스턴스에 접근할 때,
133+
노출되는 속성을 명시적으로 제한하기 위해 사용합니다.
122134

123135
```js{5,10}
124136
export default {
125137
setup(props, { expose }) {
126-
// make the instance "closed" -
127-
// i.e. do not expose anything to the parent
138+
// 인스턴스를 "닫힘" 상태로 설정
139+
// 예: 부모 컴포넌트에 아무것도 노출하지 않으려는 경우
128140
expose()
129141
130142
const publicCount = ref(0)
131143
const privateCount = ref(0)
132-
// selectively expose local state
144+
// 선택적으로 로컬 상태를 노출
133145
expose({ count: publicCount })
134146
}
135147
}
136148
```
137149

138-
## Usage with Render Functions
150+
## 렌더 함수와 함께 사용하기 {#usage-with-render-functions}
139151

140152
`setup` can also return a [render function](/guide/extras/render-function.html) which can directly make use of the reactive state declared in the same scope:
141153

154+
`setup`은 범위 내 선언된 반응형 상태에 직접 접근할 수 있는 [렌더 함수](/guide/extras/render-function.html)를 반환할 수도 있습니다:
155+
142156
```js{6}
143157
import { h, ref } from 'vue'
144158
@@ -150,9 +164,12 @@ export default {
150164
}
151165
```
152166

153-
Returning a render function prevents us from returning anything else. Internally that shouldn't be a problem, but it can be problematic if we want to expose methods of this component to the parent component via template refs.
167+
렌더 함수를 반환하면, 다른 것을 반환할 수 없습니다.
168+
내부적으로는 문제가 되지 않지만,
169+
템플릿 참조를 통해 이 컴포넌트의 메서드를 부모 컴포넌트에 노출하려는 경우,
170+
문제가 될 수 있습니다.
154171

155-
We can solve this problem by calling [`expose()`](#exposing-public-properties):
172+
이럴 때는 [`expose()`](#exposing-public-properties)를 호출하여 이 문제를 해결할 수 있습니다:
156173

157174
```js{8-10}
158175
import { h, ref } from 'vue'
@@ -171,4 +188,4 @@ export default {
171188
}
172189
```
173190

174-
The `increment` method would then be available in the parent component via a template ref.
191+
이제 `increment` 메서드는 템플릿 참조를 통해 부모 컴포넌트에서 사용할 수 있습니다.

0 commit comments

Comments
 (0)