vue插槽slot

插槽的简单使用

如果传数值 用父传子

如果传的是标签/组件 用插槽

具名插槽

老写法

具名插槽名字唯一

父子插槽一一对应

<Car>
  <h1 slot="car">性感小车不在线</h1>
  <p slot="color">我是小绿绿</p>
</Car>

<slot name="car">
    <h1>性感小车在线喷漆</h1>
</slot>

新用法v-slot

<template v-slot:car>//#car
  <h1>性感小车不在线</h1>
</template>
<template v-slot:color>//#color
  <p>我是小绿绿</p>
</template>

<slot name="car">
    <h1>性感小车在线喷漆</h1>
</slot>
<hr>
<slot name="color">
    <p>我是小粉粉</p>
</slot>

具名插槽控制显示位置

<Car>
  <template #forward>
    <button>前</button>
  </template>
  <template #back>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中的slot)是一种用于在组件中入内容的机制。它允许我们在组件的模板中定义一些占位符,然后在使用该组件时,可以将具体的内容入到这些占位符中。 在Vue 2.x版本中,我们可以使用`<slot>`元素来定义,并使用`<slot>`元素的`name`属性来定义具名。例如: ```html <template> <div> <slot></slot> <!-- 默认 --> <slot name="header"></slot> <!-- 具名 --> <slot name="footer"></slot> <!-- 具名 --> </div> </template> ``` 然后,在使用该组件时,我们可以在组件标签中入内容,这些内容将会替换对应的。例如: ```html <my-component> <p>默认的内容</p> <template v-slot:header> <h1>具名header的内容</h1> </template> <template v-slot:footer> <p>具名footer的内容</p> </template> </my-component> ``` 在Vue 3.x版本中,为了统一的语法,引入了`v-slot`指令,取代了`slot`和`slot-scope`这两个在Vue 2.x中已被废弃但仍可使用的属性。使用`v-slot`指令时,可以直接在组件标签上使用,而不需要再使用`<template>`元素。例如: ```html <my-component> <template #default> <p>默认的内容</p> </template> <template #header> <h1>具名header的内容</h1> </template> <template #footer> <p>具名footer的内容</p> </template> </my-component> ``` 需要注意的是,在Vue 3.x中,只能使用`v-slot`指令来定义,而不能再使用`slot`和`slot-scope`属性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值