Skip to content

Commit 055471c

Browse files
authored
Merge pull request #237 from imgss/patch-1
Update render-function.md
2 parents bf3f7e1 + fad450a commit 055471c

File tree

1 file changed

+38
-41
lines changed

1 file changed

+38
-41
lines changed

src/v2/guide/render-function.md

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,18 @@ Vue.component('anchored-heading', {
9393
// @returns {VNode}
9494
createElement(
9595
// {String | Object | Function}
96-
 // 一个 HTML 标签,组件选项,或一个函数
97-
// 必须 Return 上述其中一个
98-
'div',
96+
 // 一个 HTML 标签字符串,组件选项对象,或者一个返回值类型为String/Object的函数,必要参数
97+
 'div',
9998

10099
// {Object}
101-
// 一个对应属性的数据对象
102-
// 您可以在 template 中使用.可选项.
100+
 // 一个包含模板相关属性的数据对象
101+
 // 这样,您可以在 template 中使用这些属性.可选参数.
103102
{
104-
// (下一章,将详细说明相关细节)
103+
   // (详情见下一节)
105104
},
106105

107106
// {String | Array}
108-
// 子节点(VNodes). 可选项.
107+
 // 子节点(VNodes),可以是一个字符串或者一个数组. 可选参数.
109108
[
110109
createElement('h1', 'hello world'),
111110
createElement(MyComponent, {
@@ -118,9 +117,8 @@ createElement(
118117
)
119118
```
120119

121-
### 完整数据对象
122-
123-
有一件事要注意:在 templates 中,`v-bind:class``v-bind:style` ,会有特别的处理,他们在 VNode 数据对象中,为最高级配置。
120+
### 深入data object参数
121+
有一件事要注意:正如在模板语法中,`v-bind:class` 和  `v-bind:style` ,会被特别对待一样,在 VNode 数据对象中,下列属性名是级别最高的字段。
124122

125123

126124
``` js
@@ -153,13 +151,13 @@ createElement(
153151
on: {
154152
click: this.clickHandler
155153
},
156-
// 仅对于组件,用于监听原生事件,而不是组件使用 vm.$emit 触发的事件。
154+
 // 仅对于组件,用于监听原生事件,而不是组件内部使用 vm.$emit 触发的事件。
157155
nativeOn: {
158156
click: this.nativeClickHandler
159157
},
160158
// 自定义指令. 注意事项:不能对绑定的旧值设值
161-
// Vue 会为您持续追踨
162-
directives: [
159+
 // Vue 会为您持续追踪
160+
 directives: [
163161
{
164162
name: 'my-custom-directive',
165163
value: '2'
@@ -175,8 +173,8 @@ createElement(
175173
scopedSlots: {
176174
default: props => h('span', props.text)
177175
},
178-
// 如果子组件有定义 slot 的名称
179-
slot: 'name-of-slot'
176+
 // 如果组件是其他组件的子组件,需为slot指定名称
177+
 slot: 'name-of-slot'
180178
// 其他特殊顶层属性
181179
key: 'myKey',
182180
ref: 'myRef'
@@ -185,7 +183,7 @@ createElement(
185183

186184
### 完整示例
187185

188-
有了这方面的知识,我们现在可以完成我们最开始想实现的组件:
186+
有了这些知识,我们现在可以完成我们最开始想实现的组件:
189187

190188
``` js
191189
var getChildrenTextContent = function (children) {
@@ -229,14 +227,14 @@ Vue.component('anchored-heading', {
229227

230228
#### VNodes 必须唯一
231229

232-
所有组件树中的 VNodes 必须唯一。这意味着,下面的 render function 是无效的:
230+
组件树中的所有 VNodes 必须是唯一的。这意味着,下面的 render function 是无效的:
233231

234232
``` js
235233
render: function (createElement) {
236234
var myParagraphVNode = createElement('p', 'hi')
237235
return createElement('div', [
238-
// Yikes - duplicate VNodes!
239-
myParagraphVNode, myParagraphVNode
236+
   // 错误-重复的VNodes
237+
   myParagraphVNode, myParagraphVNode
240238
])
241239
}
242240
```
@@ -257,7 +255,7 @@ render: function (createElement) {
257255

258256
### `v-if` and `v-for`
259257

260-
无论什么都可以使用原生的 JavaScript 来实现,Vue 的 render 函数不会提供专用的 API。比如, template 中的 `v-if``v-for`:
258+
由于使用原生的 JavaScript 来实现某些东西很简单,Vue 的 render 函数没有提供专用的 API。比如, template 中的 `v-if``v-for`:
261259

262260
``` html
263261
<ul v-if="items.length">
@@ -281,7 +279,7 @@ render: function (createElement) {
281279

282280
### `v-model`
283281

284-
There is no direct `v-model` counterpart in render functions - you will have to implement the logic yourself:
282+
render函数中没有与`v-model`相应的api - 你必须自己来实现相应的逻辑:
285283

286284
``` js
287285
render: function (createElement) {
@@ -299,19 +297,19 @@ render: function (createElement) {
299297
}
300298
```
301299

302-
This is the cost of going lower-level, but it also gives you much more control over the interaction details compared to `v-model`.
300+
这就是深入底层要付出的,尽管麻烦了一些,但相对于 `v-model`来说,你可以更灵活地控制。
303301

304-
### Event & Key Modifiers
302+
### 事件 & 按键修饰符
305303

306-
For the `.capture` and `.once` event modifiers, Vue offers prefixes that can be used with `on`:
304+
对于 `.capture` `.once`事件修饰符, Vue 提供了相应的前缀可以用于 `on`:
307305

308306
| Modifier(s) | Prefix |
309307
| ------ | ------ |
310308
| `.capture` | `!` |
311309
| `.once` | `~` |
312310
| `.capture.once` or<br>`.once.capture` | `~!` |
313311

314-
For example:
312+
例如:
315313

316314
```javascript
317315
on: {
@@ -321,7 +319,7 @@ on: {
321319
}
322320
```
323321

324-
For all other event and key modifiers, no proprietary prefix is necessary, because you can simply use event methods in the handler:
322+
对于其他的修饰符, 前缀不是很重要, 因为你可以直接在事件处理函数中使用事件方法:
325323

326324
| Modifier(s) | Equivalent in Handler |
327325
| ------ | ------ |
@@ -331,30 +329,29 @@ For all other event and key modifiers, no proprietary prefix is necessary, becau
331329
| Keys:<br>`.enter`, `.13` | `if (event.keyCode !== 13) return` (change `13` to [another key code](http://keycode.info/) for other key modifiers) |
332330
| Modifiers Keys:<br>`.ctrl`, `.alt`, `.shift`, `.meta` | `if (!event.ctrlKey) return` (change `ctrlKey` to `altKey`, `shiftKey`, or `metaKey`, respectively) |
333331

334-
Here's an example with all of these modifiers used together:
335-
332+
这里是一个使用所有修饰符的例子:
336333
```javascript
337334
on: {
338335
keyup: function (event) {
339-
// Abort if the element emitting the event is not
340-
// the element the event is bound to
341-
if (event.target !== event.currentTarget) return
342-
// Abort if the key that went up is not the enter
343-
// key (13) and the shift key was not held down
344-
// at the same time
345-
if (!event.shiftKey || event.keyCode !== 13) return
346-
// Stop event propagation
347-
event.stopPropagation()
348-
// Prevent the default keyup handler for this element
349-
event.preventDefault()
336+
   // 如果触发事件的元素不是事件绑定的元素
337+
   // 则返回
338+
   if (event.target !== event.currentTarget) return
339+
   // 如果按下去的不是enter键或者
340+
   // 没有同时按下shift键
341+
   // 则返回
342+
   if (!event.shiftKey || event.keyCode !== 13) return
343+
   // 阻止 事件冒泡
344+
  event.stopPropagation()
345+
   // 阻止该元素默认的keyup事件
346+
   event.preventDefault()
350347
// ...
351348
}
352349
}
353350
```
354351

355-
### Slots
352+
### slots
356353

357-
You can access static slot contents as Arrays of VNodes from [`this.$slots`](http://vuejs.org/v2/api/#vm-slots):
354+
你可以从[`this.$slots`](http://vuejs.org/v2/api/#vm-slots)获取VNodes列表中的静态内容:
358355

359356
``` js
360357
render: function (createElement) {

0 commit comments

Comments
 (0)