• 【vue2第十四章】 插槽(普通插槽、具名插槽、作用域插槽语法)


    插槽

    插槽是什么?
    Vue 2 中,插槽(slot)是一种用于定义组件内部内容分发的机制。它允许你将组件中的一部分内容替换为用户自定义的内容,并在组件内部进行渲染。

    通过在组件模板中使用 标签,你可以指定一个插槽的位置。这个位置可以被父组件中的任意内容所填充。父组件中的内容将被插入到插槽所在的位置,并最终与组件的其他部分一起进行渲染。

    普通插槽

    在这里插入图片描述
    首先建立弹窗组件,在内容的位置添加 标签:

    <template>
      <div class="dialog">
        <div class="dialog-header">
          <h3>友情提示h3>
          <span class="close">✖️span>
        div>
    
        <div class="dialog-content">
          
          <slot>你确认要退出本系统么?slot>
        div>
        <div class="dialog-footer">
          <button>取消button>
          <button>确认button>
        div>
      div>
    template>
    
    <script>
    export default {
      data () {
        return {
        }
      }
    }
    script>
    
    <style scoped>
    * {
      margin: 0;
      padding: 0;
    }
    .dialog {
      width: 470px;
      height: 230px;
      padding: 0 25px;
      background-color: #ffffff;
      margin: 40px auto;
      border-radius: 5px;
    }
    .dialog-header {
      height: 70px;
      line-height: 70px;
      font-size: 20px;
      border-bottom: 1px solid #ccc;
      position: relative;
    }
    .dialog-header .close {
      position: absolute;
      right: 0px;
      top: 0px;
      cursor: pointer;
    }
    .dialog-content {
      height: 80px;
      font-size: 18px;
      padding: 15px 0;
    }
    .dialog-footer {
      display: flex;
      justify-content: flex-end;
    }
    .dialog-footer button {
      width: 65px;
      height: 35px;
      background-color: #ffffff;
      border: 1px solid #e1e3e9;
      cursor: pointer;
      outline: none;
      margin-left: 10px;
      border-radius: 3px;
    }
    .dialog-footer button:last-child {
      background-color: #007acc;
      color: #fff;
    }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    在父组件中引入组件,并且在组件标签中写入内容。如果不写将展示默认 标签里面的文字,默认内容,也叫做后备内容。

    
    <template>
      <div id="app">
        
        <MyDialog>你好!欢迎使用vue2MyDialog>
      div>
    template>
    
    
    <script>
    // import MyBodys from './components/MyBodys.vue';
    import MyDialog from './components/MyDialog.vue';
    export default {
      name: "App",
      data() {
        return {
        };
      },
      components:{
        MyDialog
      }
    };
    script>
    
    
    <style>
    #app {
      width: 100%;
      height: 700px;
      background-color: rgb(167, 167, 167);
      overflow: hidden;
    }
    *{
      margin: 0;
      padding: 0;
    }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    最后效果成功填充了弹窗中的内容:
    在这里插入图片描述

    具名插槽

    一个组件可以拥有多个插槽,每个插槽可以有不同的名称,以便在父组件中选择性地进行内容分发。父组件可以使用