0%

628_默认插槽

App组件:

<template>
  <div class="container">
    <category title="美食">
      <img
        src="https://img95.699pic.com/photo/50089/9896.jpg_wh300.jpg"
        alt=""
      />
    </category>

    <category title="游戏" :listData="game">
      <ul>
        <li v-for="(g, index) in game" :key="index">{{ g }}</li>
      </ul>
    </category>

    <category title="电影">
      <li v-for="(m, index) in movie" :key="index">{{ m }}</li>
    </category>
  </div>
</template>

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

export default {
  name: "App",
  components: { Category },
  data() {
    return {
      food: ["米饭", "面条", "烧烤", "啤酒"],
      game: ["腾讯", "网易", "元神", "lol"],
      movie: ["蜘蛛侠", "金刚", "星际", "深海"],
    };
  },
};
</script>

<style lang="css">
.container {
  display: flex;
  justify-content: space-around;
}
img {
  width: 100%;
}
</style>

Category组件:

<template>
  <div class="category">
    <h3>{{ title }}</h3>
    <!-- 定义一个插槽:(挖个坑,等着组件里面的使用者进行填充) -->
    <slot>我是一个默认值,当使用者没有使用,我会出现</slot>
    <!-- <ul>
      <li v-for="(item, index) in listData" :key="index">{{ item }}</li>
    </ul> -->
  </div>
</template>

<script>
export default {
  name: "Category",
  props: ["title"],
};
</script>

<style>
.category {
  background-color: skyblue;
  width: 200px;
  height: 300px;
}
h3 {
  text-align: center;
  background-color: orange;
}
</style>