Vue-插槽

插槽

插槽(Slot)是vue组件封装时候提供用户拓展功能,把一些不确定的功能,希望用户指定的部分定义为插槽,相当于占了一个空间给用户进行填充功能

默认插槽

1.插槽基本使用

在封装组件时,可以通过<slot>元素定义插槽,从而为用户预留内容占位符。

插槽:components/ViewA.vue

<template>
  <div>
    <h1>ViewA组件</h1>
    <div>插槽开始</div>
    <!-- 为组件的使用者预留的区域 -->
     <slot></slot>
    <div>插槽结束</div>
  </div>
</template>

<script>
export default {

}
</script>

<style>

</style>

使用组件:App.vue

<template>
  <div>
    <ViewA>
      <!-- 如果是默认插槽template可以省略 -->
      <template>
        This is a slot
      </template>
    </ViewA>
  </div>
</template>

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

<style>

</style>

 2.没有预留插槽,组件标签内的标签会被丢弃

如果封装过程没有预留<slot>插槽,用户提供自定义的内容会被丢弃,因为用户提供的内容,vue不知道往哪里放

组件:components/ViewA.vue

<template>
  <div>
    <h1>ViewA组件</h1>
    <div>插槽开始</div>
    <div>插槽结束</div>
  </div>
</template>

<script>
export default {

}
</script>

<style>

</style>

使用App.vue

<template>
  <div>
    <ViewA>
        This is a slot
    </ViewA>
  </div>
</template>

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

<style>

</style>

 3.后备内容

封装组件时候,可以预留默认内容。如果用户不往插槽提供任何内容,则该内容会生效,后备内容展示到页面上

组件:components/ViewA.vue

<template>
  <div>
    <h1>ViewA组件</h1>
    <div>插槽开始</div>
    <slot>
      我是后备内容
    </slot>
    <div>插槽结束</div>
  </div>
</template>

<script>
export default {

}
</script>

<style>

</style>

使用App.vue

<template>
  <div>
    <ViewA>
    </ViewA>
  </div>
</template>

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

<style>

</style>

 具名插槽

如果在封装组件时需要预留多个插槽节点,则需要为每个<slot>插槽指定具体的name名称,带有具有名称的插槽叫做具名插槽.

其中使用时候,template指定v-slot属性值为插槽名(eg:   v-slot:bottom),也可以简写#插槽名(eg:   #bottom)

注:没有指定name的插槽,会有隐含的名称"default"

组件:components/ViewA.vue

<template>
  <div>
    <h1>ViewA组件</h1>
    <div>插槽开始</div>
    <div>
      <slot name="header"></slot>
    </div>
    <div>
      <slot>
        我是后备内容
      </slot>
    </div>
    <div>
      <slot name="bottom"></slot>
    </div>
    <div>插槽结束</div>
  </div>
</template>

<script>
export default {

}
</script>

<style>

</style>

使用App.vue

<template>
  <div>
    <ViewA>
      <template #header>
        This is header
      </template>
      <template #default>
        This is default
      </template>
      <template v-slot:bottom>
        this is bottom
      </template>
    </ViewA>
  </div>
</template>

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

<style>

</style>

 作用域插槽

在封装组件的过程中,可以为预留的<slot> 插槽绑定 props 数据,这种带有 props 数据的<slot> 叫做“作用域插槽”。

组件:components/ViewA.vue

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in datas" :key="item.id">
          <td>{{ index + 1 }}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
          <td>
            <div>
              <!-- 后面会把这些属性组成一个对象 -->
              <slot name="checkBtn" :personInfo="item" :msg="msg1"></slot>
              <slot name="modiftyBtn" :personInfo="item" :msg="msg2"></slot>
            </div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  props: {
    datas: Array
  },
  data () {
    return {
      msg1: '查看测试',
      msg2: '修改测试'
    }
  }
}
</script>
<style></style>

使用App.vue

<template>
  <ViewA :datas="list">
    <template #checkBtn="val">
      <button @click="checkInfo(val)">查看</button>
    </template>
    <template #modiftyBtn="val">
      <button @click="modiftyInfo(val)">修改</button>
    </template>
  </ViewA>
</template>

<script>
import ViewA from './components/ViewA.vue'
export default {
  data () {
    return {
      list: [
        {
          id: 1,
          name: '123',
          age: 20
        }
      ]
    }
  },
  methods: {
    checkInfo (val) {
      console.log(val)
    },
    modiftyInfo (val) {
      console.log(val)
    }
  },
  components: {
    ViewA
  }
}
</script>

<style></style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值