vue插槽用法

一、vue插槽简单用法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <title>插槽</title>
</head>
<body>
  <div id="app">
      <div>父组件</div>
      <com>
        <!-- 插槽<slot>是否显示、怎样显示是由父组件来控制的,而插槽在哪里显示就由子组件来进行控制。 -->
          <div>
              插入子组件的标签
          </div>
      </com>
  </div>
  <script>
      var vm = new Vue({
          el:"#app",
          components: {
              "com" : {
                  template:"<h2>子组件<slot></slot></h2>"
              }
          }
      })
  </script>
</body>
</html>
页面效果:

在这里插入图片描述

二、具名插槽用法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <title>具名插槽</title>
</head>
<body>
  <div id="app">
      <com>
          <template v-slot:header>
              <h1>标题</h1>
          </template>
          <template v-slot:main>
              <h2>内容</h2>
          </template>
          <template v-slot:footer>
              <h3>底部内容</h3>
          </template>
      </com>
  </div>
  <script>
      var vm = new Vue({
          el:"#app",
          data () {
              return {
                  info:1
              }
          },
          components: {
              "com" : {
                  template:`
                  <div class="container">
                      <header>
                          <slot name="header"></slot>
                      </header>
                      <main>
                          <slot name="main"></slot>
                      </main>
                      <footer>
                          <slot name="footer"></slot>
                      </footer>
                  </div>
                  `
              }
          }
      })
  </script>
</body>
</html>
页面效果:

具名插槽

三、作用域插槽

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <title>作用域插槽</title>
</head>
<body>
  <div id="app">
      <com>
          <template v-slot:default="slotProps">
              {{ slotProps.names }} {{ slotProps.age }}
          </template>
      </com>
  </div>
  <script>
      var vm = new Vue({
          el : "#app",
          data(){
              return {
              }
          },
          components: {
              com : {
                  data(){
                      return {
                          names:"张三",
                          age:40
                      }
                  },
                  template:`<h2>子组件内容<slot :names="names" :age="age"></slot></h2>`
              }
          }
      })
  </script>
</body>
</html>
页面效果:

作用域插槽

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值