0%

628_具名插槽

App组件:

<template>
  <div class="container">
    <category title="美食">
      <img
        src="https://img95.699pic.com/photo/50089/9896.jpg_wh300.jpg"
        alt=""
        slot="center"
      />
      <a href="http://lzkpersonal.com.cn" slot="footer">我的博客</a>
    </category>

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

      <template slot="footer">
        <div class="foot">
          <a href="http://lzkpersonal.com.cn" slot="footer">单机游戏</a>
          <a href="http://lzkpersonal.com.cn" slot="footer">网络游戏</a>
        </div>
        <h4>欢迎来到我的世家</h4>
      </template>
    </category>

    <category title="电影">
      <ul slot="center">
        <li v-for="(m, index) in movie" :key="index">{{ m }}</li>
      </ul>
    </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,
.foot {
  display: flex;
  justify-content: space-around;
}
img {
  width: 100%;
}
h4 {
  text-align: center;
}
</style>

Category组件:

<template>
  <div class="category">
    <h3>{{ title }}</h3>
    <!-- 定义一个插槽:(挖个坑,等着组件里面的使用者进行填充) -->
    <slot name="center">我是一个默认值,当使用者没有使用,我会出现</slot>
    <slot name="footer">我是一个默认值,当使用者没有使用,我会出现</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>