1
- # Render Function APIs
1
+ # 渲染函数 API {#render-function- APIs}
2
2
3
- ## h()
3
+ ## h() {#h}
4
4
5
- Creates virtual DOM nodes (vnodes).
5
+ 创建虚拟 DOM 节点 (vnode)。
6
6
7
7
- ** 类型**
8
8
9
9
``` ts
10
- // full signature
10
+ // 完整参数签名
11
11
function h(
12
12
type : string | Component ,
13
13
props ? : object | null ,
14
14
children ? : Children | Slot | Slots
15
15
): VNode
16
16
17
- // omitting props
17
+ // 省略 props
18
18
function h(type : string | Component , children ? : Children | Slot ): VNode
19
19
20
20
type Children = string | number | boolean | VNode | null | Children []
@@ -28,76 +28,76 @@ Creates virtual DOM nodes (vnodes).
28
28
29
29
- **详细信息**
30
30
31
- The first argument can either be a string (for native elements) or a Vue component definition. The second argument is the props to be passed, and the third argument is the children.
31
+ 第一个参数既可以是一个字符串 (用于原生元素) 也可以是一个 Vue 组件定义。第二个参数是要传递的 prop,第三个参数是子节点。
32
32
33
- When creating a component vnode, the children must be passed as slot functions. A single slot function can be passed if the component expects only the default slot. Otherwise, the slots must be passed as an object of slot functions.
33
+ 当创建一个组件的 vnode 时,子节点必须以插槽函数进行传递。如果组件只有默认槽,可以使用单个插槽函数进行传递。否则,必须以插槽函数的对象形式来传递。
34
34
35
- For convenience, the props argument can be omitted when the children is not a slots object.
35
+ 为了方便阅读,当子节点不是插槽对象时,可以省略 prop 参数。
36
36
37
37
- **示例**
38
38
39
- Creating native elements:
39
+ 创建原生元素:
40
40
41
41
` ` ` js
42
42
import { h } from ' vue'
43
43
44
- // all arguments except the type are optional
44
+ // 除了 type 外,其他参数都是可选的
45
45
h (' div' )
46
46
h (' div' , { id: ' foo' })
47
47
48
- // both attributes and properties can be used in props
49
- // Vue automatically picks the right way to assign it
48
+ // attribute 和 property 都可以用于 prop
49
+ // Vue 会自动选择正确的方式来分配它
50
50
h (' div' , { class: ' bar' , innerHTML: ' hello' })
51
51
52
- // class and style have the same object / array
53
- // value support like in templates
52
+ // class 与 style 可以像在模板中一样
53
+ // 用数组或对象的形式书写
54
54
h (' div' , { class: [foo , { bar }], style: { color: ' red' } })
55
55
56
- // event listeners should be passed as onXxx
56
+ // 事件监听器应以 onXxx 的形式书写
57
57
h (' div' , { onClick : () => {} })
58
58
59
- // children can be a string
59
+ // children 可以是一个字符串
60
60
h (' div' , { id: ' foo' }, ' hello' )
61
61
62
- // props can be omitted when there are no props
62
+ // 没有 prop 时可以省略不写
63
63
h (' div' , ' hello' )
64
64
h (' div' , [h (' span' , ' hello' )])
65
65
66
- // children array can contain mixed vnodes and strings
66
+ // children 数组可以同时包含 vnode 和字符串
67
67
h (' div' , [' hello' , h (' span' , ' hello' )])
68
68
```
69
69
70
- Creating components:
70
+ 创建组件:
71
71
72
72
``` js
73
73
import Foo from ' ./Foo.vue'
74
74
75
- // passing props
75
+ // 传递 prop
76
76
h (Foo, {
77
- // equivalent of some-prop="hello"
77
+ // 等价于 some-prop="hello"
78
78
someProp: ' hello' ,
79
- // equivalent of @update="() => {}"
79
+ // 等价于 @update="() => {}"
80
80
onUpdate : () => {}
81
81
})
82
82
83
- // passing single default slot
83
+ // 传递单个默认插槽
84
84
h (Foo, () => ' default slot' )
85
85
86
- // passing named slots
87
- // notice the `null` is required to avoid
88
- // slots object being treated as props
86
+ // 传递具名插槽
87
+ // 注意,需要使用 `null` 来避免
88
+ // 插槽对象被当作是 prop
89
89
h (MyComponent, null , {
90
90
default : () => ' default slot' ,
91
91
foo : () => h (' div' , ' foo' ),
92
92
bar : () => [h (' span' , ' one' ), h (' span' , ' two' )]
93
93
})
94
94
```
95
95
96
- - ** 相关内容: ** [ Guide - Creating VNodes ] ( /guide/extras/render-function.html#creating-vnodes )
96
+ - ** 相关内容** : [ 指南 - 创建 VNode ] ( /guide/extras/render-function.html#creating-vnodes )
97
97
98
- ## mergeProps()
98
+ ## mergeProps() {#mergeprops}
99
99
100
- Merge multiple props objects with special handling for certain props.
100
+ 合并多个 prop 对象,用于处理含有特定的 prop 参数的情况。
101
101
102
102
- ** 类型**
103
103
@@ -107,13 +107,13 @@ Merge multiple props objects with special handling for certain props.
107
107
108
108
- ** 详细信息**
109
109
110
- ` mergeProps() ` supports merging multiple props objects with special handling for the following props :
110
+ ` mergeProps() ` 支持以下特定 prop 参数的处理,将它们合并成一个对象。
111
111
112
112
- ` class `
113
113
- ` style `
114
- - ` onXxx ` event listeners - multiple listeners with the same name will be merged into an array .
114
+ - ` onXxx ` 事件监听器——多个同名的事件监听器将被合并到一个数组。
115
115
116
- If you do not need the merge behavior and want simple overwrites , native object spread can be used instead .
116
+ 如果你不需要合并行为而是简单覆盖,可以使用原生 object spread 语法来代替。
117
117
118
118
- ** 示例**
119
119
@@ -139,9 +139,9 @@ Merge multiple props objects with special handling for certain props.
139
139
*/
140
140
` ` `
141
141
142
- ## cloneVNode ()
142
+ ## cloneVNode () {# clonevnode }
143
143
144
- Clones a vnode .
144
+ 克隆一个 vnode 。
145
145
146
146
- ** 类型**
147
147
@@ -151,11 +151,11 @@ Clones a vnode.
151
151
152
152
- ** 详细信息**
153
153
154
- Returns a cloned vnode , optionally with extra props to merge with the original .
154
+ 返回一个克隆的 vnode ,可在原有基础上添加一些额外的 prop 。
155
155
156
- Vnodes should be considered immutable once created , and you should not mutate the props of an existing vnode . Instead , clone it with different / extra props .
156
+ Vnode 被认为是一旦创建就不能修改的,你不应该修改已创建的 vnode 的 prop ,而应该附带不同的 / 额外的 prop 来克隆它。
157
157
158
- Vnodes have special internal properties , so cloning them is not as simple as an object spread . ` cloneVNode() ` handles most of the internal logic .
158
+ Vnode 具有特殊的内部 property ,因此克隆它并不像 object spread 一样简单。 ` cloneVNode() ` 处理了大部分这样的内部逻辑。
159
159
160
160
- ** 示例**
161
161
@@ -166,19 +166,19 @@ Clones a vnode.
166
166
const cloned = cloneVNode(original, { id: 'foo' })
167
167
` ` `
168
168
169
- ## isVNode ()
169
+ ## isVNode () {# isvnode }
170
170
171
- Checks if a value is a vnode .
171
+ 判断一个值是否为 vnode 类型。
172
172
173
173
- ** 类型**
174
174
175
175
` ` ` ts
176
176
function isVNode(value: unknown): boolean
177
177
` ` `
178
178
179
- ## resolveComponent ()
179
+ ## resolveComponent () {# resolvecomponent }
180
180
181
- For manually resolving a registered component by name .
181
+ 按名称手动解析已注册的组件。
182
182
183
183
- ** 类型**
184
184
@@ -188,11 +188,11 @@ For manually resolving a registered component by name.
188
188
189
189
- ** 详细信息**
190
190
191
- ** Note : you do not need this if you can import the component directly . **
191
+ ** 备注:如果你可以直接引入组件就不需使用此方法。 **
192
192
193
- ` resolveComponent() ` must be called inside <span class =" composition-api" > either ` setup() ` or </span > the render function in order to resolve from the correct component context .
193
+ 为了能从正确的组件上下文进行解析, ` resolveComponent() ` 必须在 < span class = " composition-api" > ` setup() ` 或 < / span > 渲染函数内调用。
194
194
195
- If the component is not found , a runtime warning will be emitted , and the name string is returned .
195
+ 如果组件未找到,会抛出一个运行时警告,并返回组件名字符串。
196
196
197
197
- ** 示例**
198
198
@@ -228,11 +228,11 @@ For manually resolving a registered component by name.
228
228
229
229
< / div >
230
230
231
- - ** 相关内容: ** [ Guide - Render Functions - Components ](/ guide / extras / render - function .html#components)
231
+ - ** 相关内容** :[指南 - 渲染函数 - 组件 ](/ guide / extras / render - function .html#components)
232
232
233
- ## resolveDirective()
233
+ ## resolveDirective() {# resolvedirective }
234
234
235
- For manually resolving a registered directive by name.
235
+ 按名称手动解析已注册的指令。
236
236
237
237
- ** 类型**
238
238
@@ -242,17 +242,17 @@ For manually resolving a registered directive by name.
242
242
243
243
- ** 详细信息**
244
244
245
- ** Note: you do not need this if you can import the component directly. **
245
+ ** 备注:如果你可以直接引入组件就不需使用此方法。 **
246
246
247
- `resolveDirective()` must be called inside <span class = " composition-api" > either `setup()` or </span > the render function in order to resolve from the correct component context.
247
+ 为了能从正确的组件上下文进行解析, ` resolveDirective() ` 必须在 < span class = " composition-api" > ` setup() ` 或 < / span > 渲染函数内调用。
248
248
249
- If the directive is not found, a runtime warning will be emitted, and the function returns `undefined`.
249
+ 如果指令没有找到,会抛出一个运行时警告,并返回 ` undefined ` 。
250
250
251
- - ** 相关内容: ** [Guide - Render Functions - Custom Directives ](/guide /extras /render -function.html#custom -directives )
251
+ - ** 相关内容** :[指南 - 渲染函数 - 自定义指令 ](/ guide / extras / render - function .html#custom-directives)
252
252
253
- ## withDirectives()
253
+ ## withDirectives() {# withdirectives }
254
254
255
- For adding custom directives to vnodes.
255
+ 用于给 vnode 增加自定义指令。
256
256
257
257
- ** 类型**
258
258
@@ -273,14 +273,14 @@ For adding custom directives to vnodes.
273
273
274
274
- ** 详细信息**
275
275
276
- Wraps an existing vnode with custom directives. The second argument is an array of custom directives. Each custom directive is also represented as an array in the form of `[Directive, value, argument, modifiers]`. Tailing elements of the array can be omitted if not needed.
276
+ 用自定义指令包装一个现有的 vnode 。第二个参数是自定义指令数组。每个自定义指令也可以表示为 ` [Directive, value, argument, modifiers] ` 形式的数组。如果不需要,可以省略数组的尾元素。
277
277
278
278
- ** 示例**
279
279
280
280
` ` ` js
281
281
import { h, withDirectives } from 'vue'
282
282
283
- // a custom directive
283
+ // 一个自定义指令
284
284
const pin = {
285
285
mounted() {
286
286
/* ... */
@@ -296,11 +296,11 @@ For adding custom directives to vnodes.
296
296
])
297
297
` ` `
298
298
299
- - **相关内容:** [Guide - Render Functions - Custom Directives ](/guide/extras/render-function.html#custom-directives)
299
+ - ** 相关内容** :[指南 - 渲染函数 - 自定义指令 ](/ guide / extras / render - function .html#custom-directives)
300
300
301
- ## withModifiers()
301
+ ## withModifiers() {# withmodifiers }
302
302
303
- For adding built-in [` v - on ` modifiers ](/guide/essentials/event-handling.html#event-modifiers) to an event handler function.
303
+ 用于向事件处理函数添加内置 [` v-on ` 修饰符 ](/ guide / essentials / event - handling .html #event - modifiers )。
304
304
305
305
- ** 类型**
306
306
@@ -314,11 +314,11 @@ For adding built-in [`v-on` modifiers](/guide/essentials/event-handling.html#eve
314
314
import { h, withModifiers } from 'vue'
315
315
316
316
const vnode = h('button', {
317
- // equivalent of v-on.stop.prevent
317
+ // 等价于 v-on.stop.prevent
318
318
onClick: withModifiers(() => {
319
319
// ...
320
320
}, ['stop', 'prevent'])
321
321
})
322
322
` ` `
323
323
324
- - **相关内容:** [Guide - Render Functions - Event Modifiers ](/guide/extras/render-function.html#event-modifiers)
324
+ - ** 相关内容** :[指南 - 渲染函数 - 事件修饰符 ](/ guide / extras / render - function .html#event-modifiers)
0 commit comments