16
16
- [ TSX] ( #tsx )
17
17
- [ 限制] ( #限制 )
18
18
- [ API] ( https://vue-composition-api-rfc.netlify.com/api.html )
19
- - [ Changelog ] ( https://github.com/vuejs/composition-api/blob/master/CHANGELOG.md )
19
+ - [ 更新日志 ] ( https://github.com/vuejs/composition-api/blob/master/CHANGELOG.md )
20
20
21
21
# 安装
22
22
@@ -55,9 +55,9 @@ Vue.use(VueCompositionApi);
55
55
56
56
# TypeScript
57
57
58
- ** 请使用最新版的 TypeScript,如果你使用了 ` vetur ` ,请将 ` vetur.useWorkspaceDependencies ` 设为 ` true ` 。**
58
+ ** 本插件要求使用 TypeScript 3.5.1 以上版本,如果你正在使用 ` vetur ` ,请将 ` vetur.useWorkspaceDependencies ` 设为 ` true ` 。**
59
59
60
- 为了让 TypeScript 正确的推导类型 ,我们必须使用 ` defineComponent ` 来定义组件:
60
+ 为了让 TypeScript 在 Vue 组件选项中正确地推导类型 ,我们必须使用 ` defineComponent ` 来定义组件:
61
61
62
62
``` ts
63
63
import { defineComponent } from ' @vue/composition-api' ;
@@ -79,7 +79,7 @@ const Component = {
79
79
要支持 TSX,请创建一个类型定义文件并提供正确的 JSX 定义。内容如下:
80
80
81
81
``` ts
82
- // file: shim-tsx.d.ts`
82
+ // 文件: ` shim-tsx.d.ts`
83
83
import Vue , { VNode } from ' vue' ;
84
84
import { ComponentRenderProxy } from ' @vue/composition-api' ;
85
85
@@ -90,7 +90,7 @@ declare global {
90
90
// tslint:disable no-empty-interface
91
91
interface ElementClass extends ComponentRenderProxy {}
92
92
interface ElementAttributesProperty {
93
- $props: any ; // specify the property name to use
93
+ $props: any ; // 定义要使用的属性名称
94
94
}
95
95
interface IntrinsicElements {
96
96
[elem : string ]: any ;
@@ -101,21 +101,21 @@ declare global {
101
101
102
102
# 限制
103
103
104
- ## ` Ref ` Unwrap
104
+ ## ` Ref ` 自动展开 (unwrap)
105
105
106
- 数组索引属性无法进行自动的 ` Unwrap ` :
106
+ 数组索引属性无法进行自动展开 :
107
107
108
108
### ** 不要** 使用 ` Array ` 直接存取 ` ref ` 对象:
109
109
110
110
``` js
111
111
const state = reactive ({
112
112
list: [ref (0 )],
113
113
});
114
- // no unwrap, `.value` is required
114
+ // 不会自动展开, 须使用 `.value`
115
115
state .list [0 ].value === 0 ; // true
116
116
117
117
state .list .push (ref (1 ));
118
- // no unwrap, `.value` is required
118
+ // 不会自动展开, 须使用 `.value`
119
119
state .list [1 ].value === 1 ; // true
120
120
```
121
121
@@ -126,23 +126,23 @@ const a = {
126
126
count: ref (0 ),
127
127
};
128
128
const b = reactive ({
129
- list: [a], // a.count will not unwrap !!
129
+ list: [a], // ` a.count` 不会自动展开 !!
130
130
});
131
131
132
- // no unwrap for `count`, `.value` is required
132
+ // `count` 不会自动展开, 须使用 `.value`
133
133
b .list [0 ].count .value === 0 ; // true
134
134
```
135
135
136
136
``` js
137
137
const b = reactive ({
138
138
list: [
139
139
{
140
- count: ref (0 ), // no unwrap !!
140
+ count: ref (0 ), // 不会自动展开 !!
141
141
},
142
142
],
143
143
});
144
144
145
- // no unwrap for `count`, `.value` is required
145
+ // `count` 不会自动展开, 须使用 `.value`
146
146
b .list [0 ].count .value === 0 ; // true
147
147
```
148
148
@@ -155,15 +155,15 @@ const a = reactive({
155
155
const b = reactive ({
156
156
list: [a],
157
157
});
158
- // unwrapped
158
+ // 自动展开
159
159
b .list [0 ].count === 0 ; // true
160
160
161
161
b .list .push (
162
162
reactive ({
163
163
count: ref (1 ),
164
164
})
165
165
);
166
- // unwrapped
166
+ // 自动展开
167
167
b .list [1 ].count === 1 ; // true
168
168
```
169
169
@@ -180,11 +180,11 @@ b.list[1].count === 1; // true
180
180
181
181
---
182
182
183
- ## Template Refs
183
+ ## 模板 Refs
184
184
185
- > :white_check_mark : Support   ;  ;  ;  ; :x : Not Support
185
+ > :white_check_mark : 支持   ;  ;  ;  ; :x : 不支持
186
186
187
- :white_check_mark : String ref && return it from ` setup() ` :
187
+ :white_check_mark : 字符串 ref && 从 ` setup() ` 返回 ref :
188
188
189
189
``` html
190
190
<template >
@@ -197,7 +197,7 @@ b.list[1].count === 1; // true
197
197
const root = ref (null );
198
198
199
199
onMounted (() => {
200
- // the DOM element will be assigned to the ref after initial render
200
+ // 在初次渲染后 DOM 元素会被赋值给 ref
201
201
console .log (root .value ); // <div/>
202
202
});
203
203
@@ -209,15 +209,15 @@ b.list[1].count === 1; // true
209
209
</script >
210
210
```
211
211
212
- :white_check_mark : String ref && return it from ` setup() ` && Render Function / JSX:
212
+ :white_check_mark : 字符串 ref && 从 ` setup() ` 返回 ref && 渲染函数 / JSX:
213
213
214
214
``` jsx
215
215
export default {
216
216
setup () {
217
217
const root = ref (null );
218
218
219
219
onMounted (() => {
220
- // the DOM element will be assigned to the ref after initial render
220
+ // 在初次渲染后 DOM 元素会被赋值给 ref
221
221
console .log (root .value ); // <div/>
222
222
});
223
223
@@ -226,13 +226,13 @@ export default {
226
226
};
227
227
},
228
228
render () {
229
- // with JSX
229
+ // 使用 JSX
230
230
return () => < div ref= " root" / > ;
231
231
},
232
232
};
233
233
```
234
234
235
- :x : Function ref:
235
+ :x : 函数 ref:
236
236
237
237
``` html
238
238
<template >
@@ -252,7 +252,7 @@ export default {
252
252
</script >
253
253
```
254
254
255
- :x : Render Function / JSX:
255
+ :x : 渲染函数 / JSX:
256
256
257
257
``` jsx
258
258
export default {
@@ -264,7 +264,7 @@ export default {
264
264
ref: root,
265
265
});
266
266
267
- // with JSX
267
+ // 使用 JSX
268
268
return () => < div ref= {root} / > ;
269
269
},
270
270
};
@@ -279,7 +279,7 @@ export default {
279
279
setup (initProps , setupContext ) {
280
280
const refs = setupContext .refs ;
281
281
onMounted (() => {
282
- // the DOM element will be assigned to the ref after initial render
282
+ // 在初次渲染后 DOM 元素会被赋值给 ref
283
283
console .log (refs .root ); // <div/>
284
284
});
285
285
@@ -288,7 +288,7 @@ export default {
288
288
ref: ' root' ,
289
289
});
290
290
291
- // with JSX
291
+ // 使用 JSX
292
292
return () => < div ref= " root" / > ;
293
293
},
294
294
};
0 commit comments