0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Vue] コンポーネント スロット

0
Posted at

Vue公式のチュートリアル
スロット

これも、コンポーネント関係の話。

props を経由したデータの受け渡しだけでなく、親コンポーネントはテンプレートフラグメントを スロット を経由して子コンポーネントへ渡すこともできます:

親側で呼び出し

<ChildComp>
  This is some slot content!
</ChildComp>

子側ではこのように書く。

<!-- in child template -->
<slot/>

slot の中のコンテンツは、親コンポーネントがスロットコンテンツを渡さなかった場合に表示される。

<slot>Fallback content</slot>

stackblitzで書いた。

親側で、<ChildComp></ChildComp> 内に、何も書かない状態だと、

<ChildComp>
</ChildComp>

slot の中のコンテンツが表示される。
スクリーンショット 2026-06-25 184947.png

親側で、<ChildComp></ChildComp> 内に、何か書いて渡すと、

<script setup>
import { ref } from 'vue';
import ChildComp from './ChildComp.vue';

const msg = ref('from parent');
</script>

<template>
  <ChildComp>Message: {{ msg }}</ChildComp>
</template>

<ChildComp></ChildComp>内に書いたものが表示される。
スクリーンショット 2026-06-25 185922.png

子側で、slot 部分を3つに増やしてみたら、

<template>
  <p><slot>Fallback content</slot></p>
  <p><slot>Fallback content</slot></p>
  <p><slot>Fallback content</slot></p>
</template>

<ChildComp></ChildComp>内に書いたものが、3つ表示される。

スクリーンショット 2026-06-25 190409.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?