|
4 | 4 |
|
5 | 5 | ## `setup`
|
6 | 6 |
|
7 |
| -A component option that is executed **before** the component is created, once the `props` are resolved, and serves as the entry point for composition API's |
| 7 | +A component option that is executed **before** the component is created, once the `props` are resolved. It serves as the entry point for composition APIs. |
8 | 8 |
|
9 | 9 | - **Arguments:**
|
10 | 10 |
|
11 | 11 | - `{Data} props`
|
12 | 12 | - `{SetupContext} context`
|
13 | 13 |
|
| 14 | + Similar to `this.$props` when using Options API, the `props` object will only contain explicitly declared props. Also, all declared prop keys will be present on the `props` object, regardless of whether it was passed by the parent component or not. Absent optional props will have a value of `undefined`. |
| 15 | + |
| 16 | + If you need to check the absence of an optional prop, you can give it a Symbol as its default value: |
| 17 | + |
| 18 | + ```js |
| 19 | + const isAbsent = Symbol() |
| 20 | + |
| 21 | + export default { |
| 22 | + props: { |
| 23 | + foo: { default: isAbsent } |
| 24 | + }, |
| 25 | + setup(props) { |
| 26 | + if (props.foo === isAbsent) { |
| 27 | + // foo was not provided. |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + ``` |
| 32 | + |
14 | 33 | - **Typing**:
|
15 | 34 |
|
16 |
| -```ts |
17 |
| -interface Data { |
18 |
| - [key: string]: unknown |
19 |
| -} |
| 35 | + ```ts |
| 36 | + interface Data { |
| 37 | + [key: string]: unknown |
| 38 | + } |
20 | 39 |
|
21 |
| -interface SetupContext { |
22 |
| - attrs: Data |
23 |
| - slots: Slots |
24 |
| - emit: (event: string, ...args: unknown[]) => void |
25 |
| -} |
| 40 | + interface SetupContext { |
| 41 | + attrs: Data |
| 42 | + slots: Slots |
| 43 | + emit: (event: string, ...args: unknown[]) => void |
| 44 | + } |
26 | 45 |
|
27 |
| -function setup(props: Data, context: SetupContext): Data |
28 |
| -``` |
| 46 | + function setup(props: Data, context: SetupContext): Data |
| 47 | + ``` |
29 | 48 |
|
30 |
| -::: tip |
31 |
| -To get type inference for the arguments passed to `setup()`, the use of [defineComponent](global-api.html#definecomponent) is needed. |
32 |
| -::: |
| 49 | + ::: tip |
| 50 | + To get type inference for the arguments passed to `setup()`, the use of [defineComponent](global-api.html#definecomponent) is needed. |
| 51 | + ::: |
33 | 52 |
|
34 | 53 | - **Example**
|
35 | 54 |
|
@@ -129,48 +148,52 @@ The component instance context is also set during the synchronous execution of l
|
129 | 148 |
|
130 | 149 | - **Typing**:
|
131 | 150 |
|
132 |
| -```ts |
133 |
| -interface InjectionKey<T> extends Symbol {} |
134 |
| -
|
135 |
| -function provide<T>(key: InjectionKey<T> | string, value: T): void |
136 |
| -
|
137 |
| -// without default value |
138 |
| -function inject<T>(key: InjectionKey<T> | string): T | undefined |
139 |
| -// with default value |
140 |
| -function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T |
141 |
| -// with factory |
142 |
| -function inject<T>( |
143 |
| - key: InjectionKey<T> | string, |
144 |
| - defaultValue: () => T, |
145 |
| - treatDefaultAsFactory: true |
146 |
| -): T |
147 |
| -``` |
| 151 | + ```ts |
| 152 | + interface InjectionKey<T> extends Symbol {} |
| 153 | +
|
| 154 | + function provide<T>(key: InjectionKey<T> | string, value: T): void |
| 155 | +
|
| 156 | + // without default value |
| 157 | + function inject<T>(key: InjectionKey<T> | string): T | undefined |
| 158 | + // with default value |
| 159 | + function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T |
| 160 | + // with factory |
| 161 | + function inject<T>( |
| 162 | + key: InjectionKey<T> | string, |
| 163 | + defaultValue: () => T, |
| 164 | + treatDefaultAsFactory: true |
| 165 | + ): T |
| 166 | + ``` |
148 | 167 |
|
149 |
| -Vue provides an `InjectionKey` interface which is a generic type that extends `Symbol`. It can be used to sync the type of the injected value between the provider and the consumer: |
| 168 | + Vue provides an `InjectionKey` interface which is a generic type that extends `Symbol`. It can be used to sync the type of the injected value between the provider and the consumer: |
150 | 169 |
|
151 |
| -```ts |
152 |
| -import { InjectionKey, provide, inject } from 'vue' |
| 170 | + ```ts |
| 171 | + import { InjectionKey, provide, inject } from 'vue' |
153 | 172 |
|
154 |
| -const key: InjectionKey<string> = Symbol() |
| 173 | + const key: InjectionKey<string> = Symbol() |
155 | 174 |
|
156 |
| -provide(key, 'foo') // providing non-string value will result in error |
| 175 | + provide(key, 'foo') // providing non-string value will result in error |
157 | 176 |
|
158 |
| -const foo = inject(key) // type of foo: string | undefined |
159 |
| -``` |
| 177 | + const foo = inject(key) // type of foo: string | undefined |
| 178 | + ``` |
160 | 179 |
|
161 |
| -If using string keys or non-typed symbols, the type of the injected value will need to be explicitly declared: |
| 180 | + If using string keys or non-typed symbols, the type of the injected value will need to be explicitly declared: |
162 | 181 |
|
163 |
| -```ts |
164 |
| -const foo = inject<string>('foo') // string | undefined |
165 |
| -``` |
| 182 | + ```ts |
| 183 | + const foo = inject<string>('foo') // string | undefined |
| 184 | + ``` |
166 | 185 |
|
167 | 186 | - **See also**:
|
168 | 187 | - [Provide / Inject](../guide/component-provide-inject.html)
|
169 | 188 | - [Composition API Provide / Inject](../guide/composition-api-provide-inject.html)
|
170 | 189 |
|
171 | 190 | ## `getCurrentInstance`
|
172 | 191 |
|
173 |
| -`getCurrentInstance` enables access to an internal component instance useful for advanced usages or for library creators. |
| 192 | +`getCurrentInstance` enables access to an internal component instance. |
| 193 | + |
| 194 | +:::warning |
| 195 | +`getCurrentInstance` is only exposed for advanced use cases, typically in libraries. Usage of `getCurrentInstance` is strongly discouraged in application code. Do **NOT** use it as an escape hatch to get the equivalent of `this` in Composition API. |
| 196 | +::: |
174 | 197 |
|
175 | 198 | ```ts
|
176 | 199 | import { getCurrentInstance } from 'vue'
|
|
0 commit comments