Skip to content

Commit 918f835

Browse files
authored
format: repair speech problems & format (#458)
1 parent 91c6106 commit 918f835

File tree

1 file changed

+32
-44
lines changed

1 file changed

+32
-44
lines changed

README.zh-CN.md

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# @vue/composition-api
22

3-
Vue 2 插件用于提供 Vue 3 中的 **组合式 API**.
3+
用于提供 **组合式 API** 的 Vue 2 插件.
44

55
[![npm](https://img.shields.io/npm/v/@vue/composition-api)](https://www.npmjs.com/package/@vue/composition-api)
66
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/vuejs/composition-api/Build%20&%20Test)](https://github.com/vuejs/composition-api/actions?query=workflow%3A%22Build+%26+Test%22)
77

88
[English](./README.md) | 中文 ・ [**组合式 API 文档**](https://composition-api.vuejs.org/zh)
99

10-
1110
## 安装
1211

1312
### NPM
@@ -18,7 +17,7 @@ npm install @vue/composition-api
1817
yarn add @vue/composition-api
1918
```
2019

21-
在使用 `@vue/composition-api` 前,必须先先通过 `Vue.use()` 进行安装后方可使用使用新的 [**组合式 API**](https://composition-api.vuejs.org/zh) 进行组件开发。
20+
在使用 `@vue/composition-api` 前,必须先通过 `Vue.use()` 进行安装。之后才可使用新的 [**组合式 API**](https://composition-api.vuejs.org/zh) 进行组件开发。
2221

2322
```js
2423
import Vue from 'vue'
@@ -34,16 +33,17 @@ import { ref, reactive } from '@vue/composition-api'
3433

3534
> :bulb: 当迁移到 Vue 3 时,只需简单的将 `@vue/composition-api` 替换成 `vue` 即可。你现有的代码几乎无需进行额外的改动。
3635
37-
3836
### CDN
3937

4038
在 Vue 之后引入 `@vue/composition-api` ,插件将会自动完成安装。
4139

4240
<!--cdn-links-start-->
41+
4342
```html
4443
<script src="https://cdn.jsdelivr.net/npm/vue@2.6"></script>
4544
<script src="https://cdn.jsdelivr.net/npm/@vue/composition-api@1.0.0-beta.6"></script>
4645
```
46+
4747
<!--cdn-links-end-->
4848

4949
`@vue/composition-api` 将会暴露在全局变量 `window.VueCompositionAPI` 中。
@@ -56,7 +56,7 @@ const { ref, reactive } = VueCompositionAPI
5656

5757
> 本插件要求使用 TypeScript **3.5.1** 或以上版本
5858
59-
为了让 TypeScript 在 Vue 组件选项中正确地推导类型,我们必须使用 `defineComponent` 来定义组件:
59+
为了让 TypeScript 在 Vue 组件选项中正确地进行类型推导,我们必须使用 `defineComponent` 来定义组件:
6060

6161
```ts
6262
import { defineComponent } from '@vue/composition-api'
@@ -72,7 +72,7 @@ export default defineComponent({
7272

7373
## SSR
7474

75-
尽管 Vue 3 暂时没有给出确定的 SSR 的 API,这个插件实现了 `onServerPrefetch` 生命周期钩子函数。这个钩子允许你使用在传统 API 中的 `serverPrefetch` 函数。
75+
尽管 Vue 3 暂时没有给出确定的 SSR 的 API,这个插件实现了 `onServerPrefetch` 生命周期钩子函数。这个钩子允许你使用传统 API 中的 `serverPrefetch` 函数。
7676

7777
```js
7878
import { onServerPrefetch } from '@vue/composition-api'
@@ -86,22 +86,20 @@ export default {
8686
})
8787

8888
return {
89-
result
89+
result,
9090
}
91-
}
91+
},
9292
}
9393
```
9494

9595
## 限制
9696

9797
> :white_check_mark: 支持 &nbsp;&nbsp;&nbsp;&nbsp;:x: 不支持
9898
99-
10099
### `Ref` 自动展开 (unwrap)
101100

102101
数组索引属性无法进行自动展开:
103102

104-
105103
<details>
106104
<summary>
107105
❌ <b>不要</b> 使用数组直接存取 <code>ref</code> 对象
@@ -121,19 +119,17 @@ state.list[1].value === 1 // true
121119

122120
</details>
123121

124-
125122
<details>
126123
<summary>
127124
❌ <b>不要</b> 在数组中使用含有 <code>ref</code> 的普通对象
128125
</summary>
129126

130-
131127
```js
132128
const a = {
133-
count: ref(0)
129+
count: ref(0),
134130
}
135131
const b = reactive({
136-
list: [a] // `a.count` 不会自动展开!!
132+
list: [a], // `a.count` 不会自动展开!!
137133
})
138134

139135
// `count` 不会自动展开, 须使用 `.value`
@@ -144,9 +140,9 @@ b.list[0].count.value === 0 // true
144140
const b = reactive({
145141
list: [
146142
{
147-
count: ref(0) // 不会自动展开!!
148-
}
149-
]
143+
count: ref(0), // 不会自动展开!!
144+
},
145+
],
150146
})
151147

152148
// `count` 不会自动展开, 须使用 `.value`
@@ -155,29 +151,28 @@ b.list[0].count.value === 0 // true
155151

156152
</details>
157153

158-
159154
<details>
160155
<summary>
161156
✅ 在数组中,<b>应该</b> 总是将 <code>ref</code> 存放到 <code>reactive</code> 对象中
162157
</summary>
163158

164159
```js
165160
const a = reactive({
166-
count: ref(0)
161+
count: ref(0),
167162
})
168163
const b = reactive({
169-
list: [a]
164+
list: [a],
170165
})
171166
// 自动展开
172167
b.list[0].count === 0 // true
173168

174169
b.list.push(
175170
reactive({
176-
count: ref(1)
171+
count: ref(1),
177172
})
178173
)
179174
// 自动展开
180-
b.list[1].count === 1; // true
175+
b.list[1].count === 1 // true
181176
```
182177

183178
</details>
@@ -205,17 +200,15 @@ b.list[1].count === 1; // true
205200
})
206201
207202
return {
208-
root
203+
root,
209204
}
210-
}
205+
},
211206
}
212207
</script>
213208
```
214209

215210
</details>
216211

217-
218-
219212
<details>
220213
<summary>
221214
✅ 字符串 ref && 从 <code>setup()</code> 返回 ref && 渲染函数 / JSX
@@ -232,25 +225,23 @@ export default {
232225
})
233226

234227
return {
235-
root
228+
root,
236229
}
237230
},
238231
render() {
239232
// 使用 JSX
240233
return () => <div ref="root" />
241-
}
234+
},
242235
}
243236
```
244237

245238
</details>
246239

247-
248240
<details>
249241
<summary>
250242
❌ 函数 ref
251243
</summary>
252244

253-
254245
```html
255246
<template>
256247
<div :ref="el => root = el"></div>
@@ -262,16 +253,15 @@ export default {
262253
const root = ref(null)
263254
264255
return {
265-
root
256+
root,
266257
}
267-
}
258+
},
268259
}
269260
</script>
270261
```
271262

272263
</details>
273264

274-
275265
<details>
276266
<summary>
277267
❌ 在 <code>setup()</code> 中的渲染函数 / JSX
@@ -289,7 +279,7 @@ export default {
289279

290280
// 使用 JSX
291281
return () => <div ref={root} />
292-
}
282+
},
293283
}
294284
```
295285

@@ -300,7 +290,6 @@ export default {
300290
⚠️ <code>$refs</code> 访问的变通方案
301291
</summary>
302292

303-
304293
> :warning: **警告**: `SetupContext.refs` 并不属于 `Vue 3.0` 的一部分, `@vue/composition-api` 将其曝光在 `SetupContext` 中只是临时提供一种变通方案。
305294
306295
如果你依然选择在 `setup()` 中写 `render` 函数,那么你可以使用 `SetupContext.refs` 来访问模板引用,它等价于 Vue 2.x 中的 `this.$refs`:
@@ -311,7 +300,7 @@ export default {
311300
const refs = setupContext.refs
312301
onMounted(() => {
313302
// 在初次渲染后 DOM 元素会被赋值给 ref
314-
console.log(refs.root); // <div/>
303+
console.log(refs.root) // <div/>
315304
})
316305

317306
return () =>
@@ -352,7 +341,6 @@ declare module '@vue/composition-api' {
352341
353342
</details>
354343

355-
356344
### Watch
357345

358346
<details>
@@ -364,10 +352,11 @@ declare module '@vue/composition-api' {
364352
watch(
365353
() => {
366354
/* ... */
367-
}, {
355+
},
356+
{
368357
immediate: true,
369-
onTrack() {}, // 不可用
370-
onTrigger() {}, // 不可用
358+
onTrack() {}, // 不可用
359+
onTrigger() {}, // 不可用
371360
}
372361
)
373362
```
@@ -409,17 +398,16 @@ export default {
409398
data() {
410399
return {
411400
// 在模版中会成为 { a: { value: 1 } }
412-
a: ref(1)
401+
a: ref(1),
413402
}
414-
}
403+
},
415404
}
416405
```
417406

418407
</details>
419408

420-
421409
### 性能影响
422410

423-
用于 Vue 2 所提供的公共 API 的限制,`@vue/composition-api` 不可避免地引入了额外的性能开销。在非极端情况下,这并不会对你造成影响
411+
由于 Vue 2 的公共 API 的限制,`@vue/composition-api` 不可避免地引入了额外的性能开销。除非在极端情况下,否则这并不会对你造成影响
424412

425413
你可以查看这个 [跑分结果](https://antfu.github.io/vue-composition-api-benchmark-results/) 了解更多信息。

0 commit comments

Comments
 (0)