3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Vue.js】v-slotの使い方

Last updated at Posted at 2021-04-19

v-slotの使い方

// これが親コンポーネント
<template>
<div>
  <LikeHeader>
    <template v-slot:title>  // v-slotを使用する時は必ずtemplateタグで囲まなければいけない。
      <h1>こんにちは</h1>
    </template>
    <template v-slot:number>
      <h2>{{ number }}</h2>
    </template>
  </LikeHeader>
  <LikeNumber :total-number="number" @my-click="number= $event"></LikeNumber>
  <LikeNumber :total-number="number"></LikeNumber>
</div>
</template>

<script>
import LikeHeader from "./components/LikeHeader.vue";

export default {
  data() {
    return {
      number: 18
    };
  },
  components: {
    LikeHeader
  }
}
</script>
// こっちが子コンポーネント
<template>
  <div>
    <slot name="title"></slot> // 親コンポーネントのv-slotで指定したnameをこのslotタグに渡す
    <slot></slot>
    <hr>
    <p>いいねの数</p>
    <slot name="number"></slot>  
  </div>
</template>

<script>
export default {
  props: ["headerText"]
};
</script>

これでslotごとに表示を変えることができる。

親コンポーネントでv-slotを使用する時は、divタグなどで囲むとエラーが出る。
必ずtemplateタグを使用する。

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?