0%

628_作用域插槽

插槽:

让父组件可以向子组件指定位置插入html结构,也是一种组件通信的方式,适用于子传父

作用域插槽:

数据在组件自身,但数据生成的结构需要使用者来决定

App组件:

<template>
  <div class="container">
    <category title="游戏">
      <template scope="{game}">
        <!-- ES6解构赋值-->
        <ul>
          <li v-for="(g, index) in game" :key="index">{{ g }}</li>
        </ul>
      </template>
    </category>

    <category title="游戏">
      <template scope="{game}">
        <!-- ES6解构赋值-->
        <ol>
          <li v-for="(g, index) in game" :key="index">{{ g }}</li>
        </ol>
      </template>
    </category>

    <category title="游戏">
      <template scope="{game}">
        <!-- ES6解构赋值-->

        <h4 v-for="(g, index) in game" :key="index">{{ g }}</h4>
      </template>
    </category>
  </div>
</template>

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

export default {
  name: "App",
  components: { Category },
};
</script>

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

Category组件:

<template>
  <div class="container">
    <category title="游戏">
      <template scope="{game}">
        <!-- ES6解构赋值-->
        <ul>
          <li v-for="(g, index) in game" :key="index">{{ g }}</li>
        </ul>
      </template>
    </category>

    <category title="游戏">
      <template scope="{game}">
        <!-- ES6解构赋值-->
        <ol>
          <li v-for="(g, index) in game" :key="index">{{ g }}</li>
        </ol>
      </template>
    </category>

    <category title="游戏">
      <template scope="{game}">
        <!-- ES6解构赋值-->

        <h4 v-for="(g, index) in game" :key="index">{{ g }}</h4>
      </template>
    </category>
  </div>
</template>

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

export default {
  name: "App",
  components: { Category },
};
</script>

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