Skip to content
This repository was archived by the owner on Aug 8, 2022. It is now read-only.

Commit 5397b1c

Browse files
wxsmsveaba
andauthored
docs: update composition-api.md (#635)
* docs: update composition-api.md * docs: update composition-api.md * Apply suggestions from code review Co-authored-by: Godpu <Godpu@outlook.com> Co-authored-by: Godpu <Godpu@outlook.com>
1 parent 92aa105 commit 5397b1c

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/api/composition-api.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
## `setup`
66

7-
一个组件选项,在创建组件**之前**执行,一旦 `props` 被解析,并作为组合式 API 的入口点
7+
一个组件选项,在组件被创建**之前**`props` 被解析之后执行。它是组合式 API 的入口。
88

99
- **入参:**
1010

@@ -98,7 +98,7 @@
9898

9999
## 生命周期钩子
100100

101-
可以使用直接导入的 `onX` 函数注册生命周期钩子
101+
可以通过直接导入 `onX` 函数来注册生命周期钩子
102102

103103
```js
104104
import { onMounted, onUpdated, onUnmounted } from 'vue'
@@ -118,11 +118,11 @@ const MyComponent = {
118118
}
119119
```
120120

121-
这些生命周期钩子注册函数只能在 [`setup()`](#setup) 期间同步使用,因为它们依赖于内部全局状态来定位当前活动实例 (此时正在调用其 `setup()` 的组件实例)。在没有当前活动实例的情况下调用它们将导致错误
121+
这些生命周期钩子注册函数只能在 [`setup()`](#setup) 期间同步使用,因为它们依赖于内部的全局状态来定位当前活动的实例 (此时正在调用其 `setup()` 的组件实例)。在没有当前活动实例的情况下,调用它们将会出错
122122

123-
组件实例上下文也是在生命周期钩子的同步执行期间设置的,因此在生命周期钩子内同步创建的侦听器和计算属性也会在组件卸载时自动删除
123+
组件实例的上下文也是在生命周期钩子的同步执行期间设置的,因此,在生命周期钩子内同步创建的侦听器和计算属性也会在组件卸载时自动删除
124124

125-
**选项 API 生命周期选项和组合式 API 之间的映射**
125+
**选项式 API 的生命周期选项和组合式 API 之间的映射**
126126

127127
- ~~`beforeCreate`~~ -> 使用 `setup()`
128128
- ~~`created`~~ -> 使用 `setup()`
@@ -142,7 +142,7 @@ const MyComponent = {
142142

143143
## Provide / Inject
144144

145-
`provide``inject` 启用依赖注入。只有在使用当前活动实例的 [`setup()`](#setup) 期间才能调用这两者
145+
`provide``inject` 启用依赖注入。这两者只能在使用当前活动实例的 [`setup()`](#setup) 期间被调用
146146

147147
- **类型声明**
148148

@@ -163,19 +163,19 @@ const MyComponent = {
163163
): T
164164
```
165165

166-
Vue 提供了一个 `InjectionKey` 接口,该接口是扩展 `Symbol` 的泛型类型。它可用于在提供者和消费者之间同步 inject 值的类型:
166+
Vue 提供了一个 `InjectionKey` 接口,该接口是扩展了 `Symbol` 的泛型类型。它可用于在生产者和消费者之间同步 inject 值的类型:
167167

168168
```ts
169169
import { InjectionKey, provide, inject } from 'vue'
170170
171171
const key: InjectionKey<string> = Symbol()
172172
173-
provide(key, 'foo') // 提供非字符串值将导致错误
173+
provide(key, 'foo') // 若提供非字符串值将出错
174174
175175
const foo = inject(key) // foo 的类型: string | undefined
176176
```
177177

178-
如果使用字符串 key 或非类型化 symbols,则需要显式声明 inject 值的类型:
178+
如果使用了字符串 key 或非类型化的 symbol,则需要显式声明 inject 值的类型:
179179

180180
```ts
181181
const foo = inject<string>('foo') // string | undefined
@@ -190,7 +190,7 @@ const MyComponent = {
190190
`getCurrentInstance` 支持访问内部组件实例。
191191

192192
:::warning
193-
`getCurrentInstance` 只会暴露给高阶用户,通常是库作者。对 `getCurrentInstance` 的使用在应用代码里是非常不鼓励的。请**不要**把它在组合式 API 中作为获得等同于 `this` 的退路来使用
193+
`getCurrentInstance` 只暴露给高阶使用场景,典型的比如在库中。强烈反对在应用的代码中使用 `getCurrentInstance`。请**不要**把它当作在组合式 API 中获取 `this` 的替代方案来使用
194194
:::
195195

196196
```ts
@@ -212,19 +212,19 @@ const MyComponent = {
212212
```ts
213213
const MyComponent = {
214214
setup() {
215-
const internalInstance = getCurrentInstance() // 工作
215+
const internalInstance = getCurrentInstance() // 有效
216216
217-
const id = useComponentId() // 工作
217+
const id = useComponentId() // 有效
218218
219219
const handleClick = () => {
220-
getCurrentInstance() // 不工作
221-
useComponentId() // 不工作
220+
getCurrentInstance() // 无效
221+
useComponentId() // 无效
222222
223-
internalInstance // 工作
223+
internalInstance // 有效
224224
}
225225
226226
onMounted(() => {
227-
getCurrentInstance() // 工作
227+
getCurrentInstance() // 有效
228228
})
229229
230230
return () =>

0 commit comments

Comments
 (0)