You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/render-function.md
+72-70Lines changed: 72 additions & 70 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,15 @@
1
1
---
2
-
title: Render Functions
2
+
title: Render 関数
3
3
type: guide
4
4
order: 14
5
5
---
6
6
7
-
## Basics
7
+
## 基本
8
8
9
-
Vue recommends using templates to build your HTML in the vast majority of cases. There are situations however, where you really need the full programmatic power of JavaScript. That's where you can use the **render function**, a closer-to-the-compiler alternative to templates.
9
+
Vue ではほとんどの場合 HTML をビルドするためにテンプレートを使うことが推奨されます。しかし、JavaScript による完全なプログラミングパワー
That template doesn't feel great. It's not only verbose, but we're duplicating `<slot></slot>`for every heading level and will have to do the same when we add the anchor element. The whole thing is also wrapped in a useless `div`because components must contain exactly one root node.
Much simpler! Sort of. The code is shorter, but also requires greater familiarity with Vue instance properties. In this case, you have to know that when you pass children without a `slot`attribute into a component, like the `Hello world!`inside of `anchored-heading`, those children are stored on the component instance at `$slots.default`. If you haven't already, **it's recommended to read through the [instance properties API](/api/#vm-slots)before diving into render functions.**
The second thing you'll have to become familiar with is how to use template features in the `createElement`function. Here are the arguments that `createElement`accepts:
//An HTML tag name, component options, or function
98
-
//returning one of these. Required.
98
+
// HTML タグ名、コンポーネントオプションもしくは関数
99
+
//これらの一つを返します。必須です。
99
100
'div',
100
101
101
102
// {Object}
102
-
//A data object corresponding to the attributes
103
-
//you would use in a template. Optional.
103
+
//テンプレート内で使うであろう属性と一致する
104
+
//データオブジェクト。任意です。
104
105
{
105
-
// (see details in the next section below)
106
+
// (詳細は下の次のセクションをご参照ください)
106
107
},
107
108
108
109
// {String | Array}
109
-
//Children VNodes. Optional.
110
+
// VNodes の子。任意です。
110
111
[
111
112
createElement('h1', 'hello world')
112
113
createElement(MyComponent, {
@@ -119,56 +120,56 @@ createElement(
119
120
)
120
121
```
121
122
122
-
### The Data Object In-Depth
123
+
### データオブジェクト詳解
123
124
124
-
One thing to note: similar to how `v-bind:class`and`v-bind:style`have special treatment in templates, they have their own top-level fields in VNode data objects.
If you really want to duplicate the same element/component many times, you can do so with a factory function. For example, the following render function is a perfectly valid way of rendering 20 identical paragraphs:
@@ -237,9 +238,9 @@ render: function (createElement) {
237
238
}
238
239
```
239
240
240
-
## Replacing Template Features with Plain JavaScript
241
+
## 素の JavaScript を使ったテンプレート置換機能
241
242
242
-
Wherever something can be easily accomplished in plain JavaScript, Vue render functions do not provide a proprietary alternative. For example, in a template using `v-if`and`v-for`:
@@ -264,15 +265,15 @@ render: function (createElement) {
264
265
265
266
## JSX
266
267
267
-
If you're writing a lot of `render` functions, it might feel painful that we're using 14 lines above in place of this much simpler and arguably more readable template:
That's why there's a [Babel plugin](https://github.com/vuejs/babel-plugin-transform-vue-jsx)to use JSX with Vue, getting us back to a syntax that's closer to templates:
<pclass="tip">Aliasing `createElement` to `h` is a common convention you'll see in the Vue ecosystem and is actually required for JSX. If `h` is not available in the scope, your app will throw an error.</p>
The anchored heading component we created earlier is relatively simple. It doesn't manage any state, watch any state passed to it, and it has no lifecycle methods. Really, it's just a function with some props.
In cases like this, we can mark components as `functional`, which means that they're stateless (no`data`) and instanceless (no`this`context). A **functional component**looks like this:
-`data`: The entire data object passed to the component
323
-
-`parent`: A reference to the parent component
320
+
-`props`: 提供されるプロパティのオブジェクト
321
+
-`children`: 子 VNode の配列
322
+
-`slots`: slots オブジェクトを返す関数
323
+
-`data`: コンポーネントに渡される全体のデータオブジェクト
324
+
-`parent`: 親コンポーネントへの参照
324
325
325
-
After adding `functional: true`, updating the render function of our anchored heading component would simply require adding the `context` argument, updating `this.$slots.default` to `context.children`, then updating `this.level` to `context.props.level`.
Since functional components are just functions, they're much cheaper to render. They're also very useful as wrapper components. For example, when you need to:
You may wonder why we need both `slots()`and`children`. Wouldn't`slots().default`be the same as `children`? In some cases, yes - but what if you have a functional component with the following children?
@@ -379,11 +381,11 @@ You may wonder why we need both `slots()` and `children`. Wouldn't `slots().defa
379
381
</my-functional-component>
380
382
```
381
383
382
-
For this component, `children`will give you both paragraphs, `slots().default`will give you only the second, and `slots().foo`will give you only the first. Having both `children`and`slots()`therefore allows you to choose whether this component knows about a slot system or perhaps delegates that responsibility to another component by simply passing along `children`.
You may be interested to know that Vue's templates actually compile to render functions. This is an implementation detail you usually don't need to know about, but if you'd like to see how specific template features are compiled, you may find it interesting. Below is a little demo using `Vue.compile`to live-compile a template string:
0 commit comments