Skip to content

Guide > Components In-Depth > Slots の翻訳を追従 #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions src/guide/component-slots.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Vue には [Web Components spec draft](https://github.com/w3c/webcomponents/blob
<todo-button>
<!-- コンポーネントを使ってアイコンを追加 -->
<font-awesome-icon name="plus"></font-awesome-icon>
Your Profile
Add todo
</todo-button>
```

Expand Down Expand Up @@ -100,7 +100,6 @@ Vue には [Web Components spec draft](https://github.com/w3c/webcomponents/blob

> 親のテンプレート内の全てのものは親のスコープでコンパイルされ、子のテンプレート内の全てのものは子のスコープでコンパイルされる。


## フォールバックコンテンツ

スロットに対して、コンテンツがない場合にだけ描画されるフォールバック (つまり、デフォルトの) コンテンツを指定すると便利な場合があります。例えば、`<submit-button>` コンポーネントにおいて:
Expand Down Expand Up @@ -222,7 +221,8 @@ Vue には [Web Components spec draft](https://github.com/w3c/webcomponents/blob
</footer>
</div>
```
**`v-slot` は([一つの例外](#デフォルトスロットしかない場合の省略記法) を除き) `<template>` にしか指定できないことに注意してください。

**`v-slot` は([一つの例外](#デフォルトスロットしかない場合の省略記法) を除き) `<template>` にしか指定できないことに注意してください。
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「デフォルトスロット」の「デ」に差分があってページ内リンクが動作してなかったため修正しました。


## スコープ付きスロット

Expand All @@ -247,8 +247,7 @@ app.component('todo-list', {
})
```

親コンポーネントでこれをカスタマイズするために、スロットに交換してもよいでしょう:

親コンポーネントでこれをカスタマイズするために、<span v-pre>`{{ item }}`</span> を `<slot>` に置き換えたい場合があります:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原文ですが、

We might want to replace the {{ item }} with a <slot> to customize it on parent component:

となっていて、<span v-pre></span> はいらないと思います。

https://v3.vuejs.org/guide/component-slots.html#scoped-slots

Copy link
Member Author

@naokie naokie Apr 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vuejs/docs#799

ここで問題になっていた下図の現象、日本語訳でも起きちゃうんですよね。

image

image

原文はこちらです。

https://github.com/vuejs/docs-next/blob/master/src/guide/component-slots.md#L250

VuePress の hack のようなものなのですが、そのまま取り込もうと思います。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど...
了解しました!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原文と同じく、取り込みましょう!


```html
<todo-list>
Expand All @@ -259,23 +258,33 @@ app.component('todo-list', {

しかし、これは動作しません。というのも、 `item` にアクセスすることができるのは `<todo-list>` コンポーネントだけですが、ここで指定しているコンテンツは親コンポーネントで描画されるからです。

親コンポーネント内でスロットコンテンツとして `item` を使えるようにするためには、これを `<slot>` 要素の属性としてバインドします:
親コンポーネント内でスロットコンテンツとして `item` を使えるようにするためには、これを `<slot>` 要素の属性として束縛します:

```html
<ul>
<li v-for="( item, index ) in items">
<slot v-bind:item="item"></slot>
<slot :item="item"></slot>
</li>
</ul>
```

`<slot>` 要素にバインドされた属性は、 **スロットプロパティ** と呼ばれます。これで、親スコープ内で `v-slot` に値を指定して、渡されたスロットプロパティの名前を定義できます:
必要な数の属性を `slot` に束縛できます:

```html
<ul>
<li v-for="( item, index ) in items">
<slot :item="item" :index="index" :another-attribute="anotherAttribute"></slot>
</li>
</ul>
```

`<slot>` 要素に束縛された属性は、 **スロットプロパティ** と呼ばれます。これで、親スコープ内で `v-slot` に値を指定して、渡されたスロットプロパティの名前を定義できます:

```html
<todo-list>
<template v-slot:default="slotProps">
<i class="fas fa-check"></i>
<span class="green">{{ slotProps.item }}<span>
<span class="green">{{ slotProps.item }}</span>
</template>
</todo-list>
```
Expand All @@ -291,7 +300,7 @@ app.component('todo-list', {
```html
<todo-list v-slot:default="slotProps">
<i class="fas fa-check"></i>
<span class="green">{{ slotProps.item }}<span>
<span class="green">{{ slotProps.item }}</span>
</todo-list>
```

Expand All @@ -300,7 +309,7 @@ app.component('todo-list', {
```html
<todo-list v-slot="slotProps">
<i class="fas fa-check"></i>
<span class="green">{{ slotProps.item }}<span>
<span class="green">{{ slotProps.item }}</span>
</todo-list>
```

Expand All @@ -309,10 +318,9 @@ app.component('todo-list', {
```html
<!-- 不正。警告が出ます -->
<todo-list v-slot="slotProps">
<todo-list v-slot:default="slotProps">
<i class="fas fa-check"></i>
<span class="green">{{ slotProps.item }}<span>
</todo-list>
<i class="fas fa-check"></i>
<span class="green">{{ slotProps.item }}</span>

<template v-slot:other="otherSlotProps">
slotProps is NOT available here
</template>
Expand All @@ -325,13 +333,13 @@ app.component('todo-list', {
<todo-list>
<template v-slot:default="slotProps">
<i class="fas fa-check"></i>
<span class="green">{{ slotProps.item }}<span>
<span class="green">{{ slotProps.item }}</span>
</template>

<template v-slot:other="otherSlotProps">
...
</template>
</current-user>
</todo-list>
```

### スロットプロパティの分割代入
Expand All @@ -358,7 +366,7 @@ function (slotProps) {
```html
<todo-list v-slot="{ item: todo }">
<i class="fas fa-check"></i>
<span class="green">{{ todo }}<span>
<span class="green">{{ todo }}</span>
</todo-list>
```

Expand Down Expand Up @@ -387,7 +395,6 @@ function (slotProps) {

`v-on` や `v-bind` と同様に `v-slot` にも省略記法があり、引数の前のすべての部分 (`v-slot:`) を特別な記号 `#` で置き換えます。例えば、`v-slot:header` は `#header` に書き換えることができます:


```html
<base-layout>
<template #header>
Expand Down