插槽的自我记录

1.插槽的功能:

 使用组件时可以直接为其添加内容;

2.如何设置插槽?

在组件内设定插槽:<slot :name = 'header'></slot>; // name为插槽名·

​
// 插槽
<template>
  <div>
    <p>
      //slot为插槽元素,name属性有自定义值的为具名插槽,无名默认为匿名插槽
      <slot></slot>
      <slot name="a" :textContent="textContent"></slot>
    </p>
  </div>
</template>

// 插槽的内容
<template>
  <div class="home">
    <son :name="name" @ChangeFather="ChangeFather">
      // 插槽内容需要用template包裹,v-slot:绑定插入哪个槽:此处为p槽
      <template v-slot:p="props">
        <p>我是插到P槽的内容</p>
        <p>{{props.textContent}}</p>
      </template>
      <template v-slot:a>
        <p>我是插到a槽的内容</p>
      </template>
    </son>
  </div>
</template>

​

3.作用域插槽

前提:父级作用域无法获取子级作用域的内容

使用场景:插槽的内容为插槽组件的数据。

使用过程:

// 使用v-slot确定插入哪个槽:p,并指定接收插槽传递过来的属性的道具盒名称:props。   
<template v-slot:p="props">
        <p>{{props.textContent}}</p>
</template>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值