vue的slot插槽

*插槽的作用:让父组件可以向子组件指定位置插入html结构

1.默认插槽

        1.子组件:

<template>
  <div class="cate">
    <h3 class="head">{{title}}分类</h3>
    <slot></slot>
  </div>
</template>

<script>
export default {
    props:['title']
}
</script>

<style scoped>
    .head{
        text-align:center;
        font-weight: bold;
        background-color: coral;
        width: 100%;
        
    }
    .cate{
        width: 200px;
        height: 300px;
        background-color: skyblue;
        margin-left: 50px;
        margin-top: 20px;
        padding-top:15px
    }
</style>

        2.父组件:

<template>
  <div class="container">
      <category title='美食'>
          <img src='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp8.itc.cn%2Fimages01%2F20210615%2F740e6f601ddd4c9db3d9a11183dc4148.jpeg&refer=http%3A%2F%2Fp8.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649405874&t=5d197bf13838a96320fd03f12e0353fb' alt=''>
      </category>
      <category title="游戏">
          <ul>
              <li v-for="(game,index) in games" :key="index">{{game}}</li>
          </ul>
      </category>
      <category title="电影">
          <video controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video>
      </category>
  </div>
</template>

<script>
import Category from './components/Category.vue'
export default {
  components: { Category },
  data() {
      return {
          foods:['火锅','烧烤','小龙虾','牛排'],
          games:['打地鼠','俄罗斯方块','王者荣耀','英雄联盟'],
          films:['《夏洛特烦恼》','《温暖的抱抱》','《你好李焕英》','《钢铁侠》']

      }
  },

}
</script>

<style>
    video{
        width: 100%;
        height: 60%;
    }
    img{
        width: 100%;
        height: 60%;
    }
    .container{
        display:flex ;
        justify-content: space-around;
    }
</style>

2.具名插槽

        1.子组件:

<template>
  <div class="cate">
    <h3 class="head">{{title}}分类</h3>
    <slot name="center"></slot>
    <slot name="footer"></slot>
  </div>
</template>

<script>
export default {
    props:['title']
}
</script>

        2.父组件:

<template>
  <div class="container">
      <category title='美食'>
          <img slot="center" src='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp8.itc.cn%2Fimages01%2F20210615%2F740e6f601ddd4c9db3d9a11183dc4148.jpeg&refer=http%3A%2F%2Fp8.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649405874&t=5d197bf13838a96320fd03f12e0353fb' alt=''>
        <a class='foot' slot="footer" href="http://www.baidu.com">更多美食</a>
      </category>
      <category title="游戏">
          <ul slot="center">
              <li v-for="(game,index) in games" :key="index">{{game}}</li>
          </ul>
          <div class='foot' slot="footer" >
            <a href="http://www.baidu.com">单机游戏 </a>
            <a href="http://www.baidu.com"> 网络游戏</a>
          </div>
      </category>
      <category title="电影">
          <video slot="center" controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video>
          <div class="foot" slot="footer">
              <a href="http://www.baidu.com">经典</a>
              <a href="http://www.baidu.com">热门</a>
              <a href="http://www.baidu.com">推荐</a>
          </div>
      </category>
  </div>
</template>

<script>
import Category from './components/Category.vue'
export default {
  components: { Category },
  data() {
      return {
          foods:['火锅','烧烤','小龙虾','牛排'],
          games:['打地鼠','俄罗斯方块','王者荣耀','英雄联盟'],
          films:['《夏洛特烦恼》','《温暖的抱抱》','《你好李焕英》','《钢铁侠》']

      }
  },

}
</script>

3.作用域插槽

        1.自组件:

<template>
  <div class="cate">
    <h3 class="head">{{title}}分类</h3>
    <!-- 通过插槽传递数据 -->
    <slot :games="games"></slot>
  </div>
</template>

<script>
export default {
    props:['title'],
      data() {
      return {
          games:['打地鼠','俄罗斯方块','王者荣耀','英雄联盟'],
      }
  },
}
</script>

        2.父组件:

<template>
  <div class="container">
      <category title="游戏">
          <!--必须用template -->
          <!-- scope接收数据,接收到的是一个对象 -->
          <template scope="gamesObj">
            <ul>
              <li v-for="(game,index) in gamesObj.games" :key="index">{{game}}</li>
            </ul>
          </template>
      </category>
      <category title="游戏">
          <!--必须用template -->
          <!-- scope接收数据,接收到的是一个对象 -->
          <template scope="gamesObj">
            <ol>
              <li v-for="(game,index) in gamesObj.games" :key="index">{{game}}</li>
            </ol>
          </template>
      </category>
            <category title="游戏">
          <!--必须用template -->
          <!-- scope接收数据,接收到的是一个对象 -->
          <template scope="gamesObj">
            <ul>
              <h4 v-for="(game,index) in gamesObj.games" :key="index">{{game}}</h4>
            </ul>
          </template>
      </category>
  </div>
</template>

<script>
import Category from './components/Category.vue'
export default {
  components: { Category }
}
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桃桃tao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值