Skip to content

Commit 819e628

Browse files
tejitakkazupon
authored andcommitted
Traslate "Render Functions" (vuejs#170)
* traslate render-funciton guide * update words by review comment
1 parent 11655d2 commit 819e628

File tree

1 file changed

+72
-70
lines changed

1 file changed

+72
-70
lines changed

src/guide/render-function.md

Lines changed: 72 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
---
2-
title: Render Functions
2+
title: Render 関数
33
type: guide
44
order: 14
55
---
66

7-
## Basics
7+
## 基本
88

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 による完全なプログラミングパワー
10+
を必要とするシチュエーションもあります。そこでテンプレートへの代替で、よりコンパイラに近い **Render 関数**が使用できます。
1011

11-
Let's dive into a simple example where a `render` function would be practical. Say you want to generate anchored headings:
12+
`render` 関数が実用的になりうる簡単な例を見てみましょう。では、アンカーヘッダを生成したいとします。
1213

1314
``` html
1415
<h1>
@@ -18,13 +19,13 @@ Let's dive into a simple example where a `render` function would be practical. S
1819
</h1>
1920
```
2021

21-
For the HTML above, you decide you want this component interface:
22+
上記の HTML に対して、望ましいコンポーネントのインターフェースを決めましょう。
2223

2324
``` html
2425
<anchored-heading :level="1">Hello world!</anchored-heading>
2526
```
2627

27-
When you get started with a component that just generates a heading based on the `level` prop, you quickly arrive at this:
28+
`level` プロパティを元にしてヘッダを生成するだけのコンポーネントから始める時、すぐに以下の例に辿りつくでしょう。
2829

2930
``` html
3031
<script type="text/x-template" id="anchored-heading-template">
@@ -63,16 +64,16 @@ Vue.component('anchored-heading', {
6364
})
6465
```
6566

66-
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.
67+
このテンプレートは良い気がしません。冗長なだけでなく、全てのヘッダレベルで `<slot></slot>` を重複させており、アンカー要素を追加したい時に同じことをする必要があります。コンポーネントは 1 つの root ノードのみ含めることができるので、全体もまた無駄な `div` で囲まれています。
6768

68-
While templates work great for most components, it's clear that this isn't one of them. So let's try rewriting it with a `render` function:
69+
テンプレートはほとんどのコンポーネントでうまく動作しますが、この例はそうではないことが明確です。では、 `render` 関数を使ってこれを書き直してみましょう。
6970

7071
``` js
7172
Vue.component('anchored-heading', {
7273
render: function (createElement) {
7374
return createElement(
74-
'h' + this.level, // tag name
75-
this.$slots.default // array of children
75+
'h' + this.level, // タグ名
76+
this.$slots.default // 子の配列
7677
)
7778
},
7879
props: {
@@ -84,29 +85,29 @@ Vue.component('anchored-heading', {
8485
})
8586
```
8687

87-
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.**
88+
少しシンプルになりましたね!コードは短くなりましたが、これもまた Vue インスタンスプロパティの豊富な知識を必要とします。このケースでは、 `slot` 属性無しでコンポーネントに子を渡した時に ( `anchored-heading` の内側の `Hello world!` のような) 、それらの子がコンポーネントインスタンス上の `$slots.default` にストアされているかを知っている必要があります。**もしあなたがまだ知らないなら、render 関数に進む前に、 [インスタンスプロパティAPI](/api/#vm-slots) を読むことをオススメします。**
8889

89-
## `createElement` Arguments
90+
## `createElement` 引数
9091

91-
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:
92+
あなたが精通する必要がある 2 つ目のことは `createElement` 関数の中でテンプレートの機能をどのように使うかについてです。こちらが `createElement` の受け付ける引数です。
9293

9394
``` js
9495
// @returns {VNode}
9596
createElement(
9697
// {String | Object | Function}
97-
// An HTML tag name, component options, or function
98-
// returning one of these. Required.
98+
// HTML タグ名、コンポーネントオプションもしくは関数
99+
// これらの一つを返します。必須です。
99100
'div',
100101

101102
// {Object}
102-
// A data object corresponding to the attributes
103-
// you would use in a template. Optional.
103+
// テンプレート内で使うであろう属性と一致する
104+
// データオブジェクト。任意です。
104105
{
105-
// (see details in the next section below)
106+
// (詳細は下の次のセクションをご参照ください)
106107
},
107108

108109
// {String | Array}
109-
// Children VNodes. Optional.
110+
// VNodes の子。任意です。
110111
[
111112
createElement('h1', 'hello world')
112113
createElement(MyComponent, {
@@ -119,56 +120,56 @@ createElement(
119120
)
120121
```
121122

122-
### The Data Object In-Depth
123+
### データオブジェクト詳解
123124

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.
125+
1 つ注意点: `v-bind:class` `v-bind:style` がテンプレート内で特殊な扱いをされているのと同じように、それらは VNode のデータオブジェクト内で自身のトップレベルフィールドを持ちます。
125126

126127
``` js
127128
{
128-
// Same API as `v-bind:class`
129+
// `v-bind:class` と同じ API
129130
'class': {
130131
foo: true,
131132
bar: false
132133
},
133-
// Same API as `v-bind:style`
134+
// `v-bind:style` と同じ API
134135
style: {
135136
color: 'red',
136137
fontSize: '14px'
137138
},
138-
// Normal HTML attributes
139+
// 通常の HTML 属性
139140
attrs: {
140141
id: 'foo'
141142
},
142-
// Component props
143+
// コンポーネントプロパティ
143144
props: {
144145
myProp: 'bar'
145146
},
146-
// DOM properties
147+
// DOM プロパティ
147148
domProps: {
148149
innerHTML: 'baz'
149150
},
150-
// Event handlers are nested under "on", though
151-
// modifiers such as in v-on:keyup.enter are not
152-
// supported. You'll have to manually check the
153-
// keyCode in the handler instead.
151+
// v-on:keyup.enter などの修飾詞はサポートされませんが、
152+
// "on" の配下にイベントハンドラはネストされます。
153+
// その代わり、手動で keyCode をハンドラの中で
154+
// 確認することが可能です。
154155
on: {
155156
click: this.clickHandler
156157
},
157-
// For components only. Allows you to listen to
158-
// native events, rather than events emitted from
159-
// the component using vm.$emit.
158+
// コンポーネントに限って、
159+
// vm.$emit を使っているコンポーネントから emit されるイベントではなく
160+
// ネイティブのイベントを listen することができます。
160161
nativeOn: {
161162
click: this.nativeClickHandler
162163
},
163-
// Other special top-level properties
164+
// 他の特殊なトップレベルのプロパティ
164165
key: 'myKey',
165166
ref: 'myRef'
166167
}
167168
```
168169

169-
### Complete Example
170+
### 完全な例
170171

171-
With this knowledge, we can now finish the component we started:
172+
これらの知識を使えば、私たちが始めたコンポーネントを完了させることができるようになりました。
172173

173174
``` js
174175
var getChildrenTextContent = function (children) {
@@ -181,7 +182,7 @@ var getChildrenTextContent = function (children) {
181182

182183
Vue.component('anchored-heading', {
183184
render: function (createElement) {
184-
// create kebabCase id
185+
// kebabCase id の作成
185186
var headingId = getChildrenTextContent(this.$slots.default)
186187
.toLowerCase()
187188
.replace(/\W+/g, '-')
@@ -208,23 +209,23 @@ Vue.component('anchored-heading', {
208209
})
209210
```
210211

211-
### Constraints
212+
### 制約
212213

213-
#### VNodes Must Be Unique
214+
#### VNodes は一意でなければならない
214215

215-
All VNodes in the component tree must be unique. That means the following render function is invalid:
216+
コンポーネントツリーの中で全ての VNode は一意でなければなりません。つまり、以下の render 関数は不正です。
216217

217218
``` js
218219
render: function (createElement) {
219220
var myParagraphVNode = createElement('p', 'hi')
220221
return createElement('div', [
221-
// Yikes - duplicate VNodes!
222+
// うわ - 重複の VNodes
222223
myParagraphVNode, myParagraphVNode
223224
])
224225
}
225226
```
226227

227-
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:
228+
もし同じ要素 / コンポーネントを何度も重複させたい場合には、factory 関数を使うことで対応できます。例えば、次の render 関数は 20 個の一意に特定できるパラグラフをレンダリングする完全に正当な方法です。
228229

229230
``` js
230231
render: function (createElement) {
@@ -237,9 +238,9 @@ render: function (createElement) {
237238
}
238239
```
239240

240-
## Replacing Template Features with Plain JavaScript
241+
## 素の JavaScript を使ったテンプレート置換機能
241242

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`:
243+
どんなところでも素の JavaScript で簡単に物事は成し遂げることができるので、Vue render 関数は独自の代替手段を提供しません。例えば、`v-if` `v-for` を使っているテンプレート内です。
243244

244245
``` html
245246
<ul v-if="items.length">
@@ -248,7 +249,7 @@ Wherever something can be easily accomplished in plain JavaScript, Vue render fu
248249
<p v-else>No items found.</p>
249250
```
250251

251-
This could be rewritten with JavaScript's `if`/`else` and `map` in a render function:
252+
これは render 関数の中で JavaScript`if` / `else` `map` を使って書き換えることができます。
252253

253254
``` js
254255
render: function (createElement) {
@@ -264,15 +265,15 @@ render: function (createElement) {
264265

265266
## JSX
266267

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:
268+
もしあなたが多くの render 関数を書いている場合は、よりシンプルでほぼ間違いなく読み易いテンプレート JSX の代わりに上記の 14 行を使っていることがつらいと感じるかもしれません。
268269

269270
``` html
270271
<anchored-heading :level="1">
271272
<span>Hello</span> world!
272273
</anchored-heading>
273274
```
274275

275-
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:
276+
そのような理由から Vue で JSX を使うための [Babel プラグイン](https://github.com/vuejs/babel-plugin-transform-vue-jsx) があります。よりテンプレートに近い文法が戻ってきました。
276277

277278
``` js
278279
import AnchoredHeading from './AnchoredHeading.vue'
@@ -289,47 +290,48 @@ new Vue({
289290
})
290291
```
291292

292-
<p class="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>
293+
<p class="tip">`createElement` `h` にエイリアスしていることは、 Vue のエコシステムの中でよく見かける慣習です。そして、それは実は JSX には必須です。もし `h` がそのスコープ内で利用可能でない場合、アプリケーションはエラーを throw するでしょう。</p>
293294

294-
For more on how JSX maps to JavaScript, see the [usage docs](https://github.com/vuejs/babel-plugin-transform-vue-jsx#usage).
295+
より詳しい JSX JavaScript へのマップの仕方については、[usage docs](https://github.com/vuejs/babel-plugin-transform-vue-jsx#usage) をご参照ください。
295296

296-
## Functional Components
297+
## 関数型コンポーネント
297298

298-
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.
299+
私たちが先ほど作成したアンカーヘッダコンポーネントは比較的シンプルです。状態の管理や渡された状態の watch をしておらず、また、何もライフサイクルメソッドを持ちません。実際、これはいくつかのプロパティを持つただの関数です。
299300

300-
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:
301+
このようなケースにおいて、私たちは `関数型` としてのコンポーネントと特徴づけることができます。それは状態を持たない ( `data` が無い) でインスタンスを持たない ( `this` のコンテキストが無い) ことを意味します。**関数型コンポーネント** は次のような形式をしています。
301302

302303
``` js
303304
Vue.component('my-component', {
304305
functional: true,
305-
// To compensate for the lack of an instance,
306-
// we are now provided a 2nd context argument.
306+
// インスタンスが無いことを補うために、
307+
// 2 つ目の context 引数が提供されます。
307308
render: function (createElement, context) {
308309
// ...
309310
},
310-
// Props are optional
311+
// プロパティは任意です
311312
props: {
312313
// ...
313314
}
314315
})
315316
```
316317

317-
Everything the component needs is passed through `context`, which is an object containing:
318+
このコンポーネントが必要な全てのことは `context` を受け取ることです。それは次を含むオブジェクトです。
318319

319-
- `props`: An object of the provided props
320-
- `children`: An array of the VNode children
321-
- `slots`: A function returning a slots object
322-
- `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`: 親コンポーネントへの参照
324325

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`.
326+
`functional: true` を追加した後、私たちのアンカーヘッダコンポーネントの render 関数の更新として単に必要になるのは、
327+
`context` 引数の追加、`this.$slots.default``context.children` への更新、`this.level``context.props.level` への更新でしょう。
326328

327-
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:
329+
関数型コンポーネントはただの関数なので、レンダリングのコストは少ないです。また、ラッパーコンポーネントとしてもとても便利です。例えば、以下が必要な時に。
328330

329-
- Programmatically choose one of several other components to delegate to
330-
- Manipulate children, props, or data before passing them on to a child component
331+
- いくつかの他のコンポーネントから 1 つ delegate するためのものをプログラムで選ぶ時
332+
- 子、プロパティ、または、データを子コンポーネントへ渡る前に操作したい時
331333

332-
Here's an example of a `smart-list` component that delegates to more specific components, depending on the props passed to it:
334+
こちらが、渡されるプロパティに応じてより具体的なコンポーネントに委譲する `smart-list` コンポーネントの例です。
333335

334336
``` js
335337
var EmptyList = { /* ... */ }
@@ -368,7 +370,7 @@ Vue.component('smart-list', {
368370

369371
### `slots()` vs `children`
370372

371-
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?
373+
もしかするとなぜ `slots()` `children` の両方が必要なのか不思議に思うかもしれません。 `slots().default` `children` と同じではないのですか?いくつかのケースではそうですが、もし以下の子を持つ関数型コンポーネントの場合はどうなるでしょう。
372374

373375
``` html
374376
<my-functional-component>
@@ -379,11 +381,11 @@ You may wonder why we need both `slots()` and `children`. Wouldn't `slots().defa
379381
</my-functional-component>
380382
```
381383

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`.
384+
このコンポーネントの場合、 `children` は両方のパラグラフが与えられます。`slots().default` は 2 つ目のものだけ与えられます。そして `slots().foo` は 1 つ目のものだけです。したがって、 `children` `slots()` の両方を持つことで このコンポーネントが slot システムを知っているか、もしくは、単純に `children` を渡しておそらく他のコンポーネントへその責任を delegate するかどうかを選べるようになります。
383385

384-
## Template Compilation
386+
## テンプレートコンパイル
385387

386-
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:
388+
もしかするとあなたは Vue のテンプレートが実際に render 関数にコンパイルされることを知ることに興味を持つかもしれません。これは普段あなたは知る必要もない実装の詳細ですが、どのように特定のテンプレート機能がコンパイルされるかをもし見たいならこれに興味を持つかもしれません。以下は `Vue.compile` を使ってテンプレート文字列をその場でコンパイルをするちょっとしたデモです。
387389

388390
{% raw %}
389391
<div id="vue-compile-demo" class="demo">

0 commit comments

Comments
 (0)