Vue中slot的理解与使用

2 篇文章 0 订阅

slot是什么?

slot俗称“插槽”,可以通过其向组件内部指定位置传递内容。

slot能够解决什么问题?

在正常的开发情况下,如果存在以下子组件child.vue,

child.vue

<template>
  <div>
    Hello World
  </div>
</template>

假如我们在父组件中这样引用它

parent.vue

<template>
  <div>
    <child>Hello Vue</child>
  </div>
</template>

则渲染结果为:Hello World

即组件标签<child>Hello Vue</child>中包含的Hello Vue会被子组件模板替换掉,但开发中有时候会需要将父组件中的数据引入到子组件中去显示,此时就需要用到slot。如下,在子组件中添加插槽<slot></slot>为父组件中的Hello Vue占位,使其在此处显示。

child.vue

<template>
  <div>
    Hello World
    <slot></slot>
  </div>
</template>

渲染结果:Hello World  Hello Vue

通过上面的简单描述应该就能够大致理解slot的使用和作用。

具名插槽

具名插槽,顾名思义是带有名字slot,而上面部分提到的slot我们可以看到只是简单的<slot></slot>。

在有些时候,我们需要不止添加一个slot,此时就需要用到具名插槽,根据名称相对应的关系来定义多个插槽。

假设有这样一个需求:

BaseLayout.vue

<template>
  <div class="container">
    <header>
      <!-- 我们希望把页头放这里 -->
    </header>
    <main>
      <!-- 我们希望把主要内容放这里 -->
    </main>
    <footer>
      <!-- 我们希望把页脚放这里 -->
    </footer>
  </div>
</template>

对于这种情况,<slot>标签有一个特殊的属性:name,这个属性可以用来标记区分多个插槽

BaseLayout.vue

<template>
  <div class="container">
    <header>
      <slot name="header"></slot>
    </header>
    <main>
      <slot></slot>
    </main>
    <footer>
      <slot name="footer"></slot>
    </footer>
  </div>
</template>

在向具名插槽提供内容的时候,我们有两种方法:

1.可以在父组件的<template>元素中使用slot属性

parent.vue

<base-layout>
  <template slot="header">
    <h1>Here might be a page title</h1>
  </template>

  <p>A paragraph for the main content.</p>
  <p>And another one.</p>

  <template slot="footer">
    <p>Here's some contact info</p>
  </template>
</base-layout>

2.直接在普通元素上使用slot属性

parent.vue

<base-layout>
  <h1 slot="header">Here might be a page title</h1>

  <p>A paragraph for the main content.</p>
  <p>And another one.</p>

  <p slot="footer">Here's some contact info</p>
</base-layout>

可以看到,在这三个插槽中,我们还是保留了一个未命名插槽,这个插槽是默认插槽,也就是说它会作为所有未匹配到插槽的内容的统一出口

上述两种方式渲染出来的html都将会是

<div class="container">
  <header>
    <h1>Here might be a page title</h1>
  </header>
  <main>
    <p>A paragraph for the main content.</p>
    <p>And another one.</p>
  </main>
  <footer>
    <p>Here's some contact info</p>
  </footer>
</div>

插槽的默认内容

有的时候为插槽提供默认的内容是很有用的。例如,一个 <submit-button> 组件可能希望这个按钮的默认内容是“Submit”,但是同时允许用户覆写为“Save”、“Upload”或别的内容。

你可以在组件模板里的 <slot> 标签内部指定默认的内容来做到这一点。

<button type="submit">
  <slot>Submit</slot>
</button>

如果父组件为这个插槽提供了内容,则默认的内容会被替换掉。

作用域插槽

推荐这篇文章https://www.jianshu.com/p/a0a83029a217,看完我理解了~

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3slot可以理解为一个占位符,用于在组件插入内容。其定义和使用方法如下: 1. 定义slot 在组件定义slot可以使用`<slot></slot>`标签,如下所示: ```vue <template> <div> <h2>这是一个组件</h2> <slot></slot> </div> </template> ``` 上述代码,`<slot></slot>`标签表示一个占位符,用于在组件插入内容。 2. 使用slot使用组件时,可以使用`<template v-slot></template>`或`<slot></slot>`标签来插入内容,如下所示: ```vue <template> <div> <h2>这是一个组件</h2> <slot></slot> </div> </template> <template> <div> <my-component> <template v-slot:default> <p>这是插入的内容</p> </template> </my-component> </div> </template> ``` 上述代码,`<template v-slot:default>`标签表示在组件插入内容,`<p>这是插入的内容</p>`表示要插入的内容。 除了`<template v-slot>`标签之外,Vue3还提供了`<slot></slot>`标签来插入内容,如下所示: ```vue <template> <div> <h2>这是一个组件</h2> <slot></slot> </div> </template> <template> <div> <my-component> <p slot="default">这是插入的内容</p> </my-component> </div> </template> ``` 上述代码,`<p slot="default">这是插入的内容</p>`表示在组件插入内容,`slot="default"`表示要插入的内容所对应的slot名称。 总结: Vue3的slot可以用于在组件插入内容,其定义和使用方法分别为在组件使用`<slot></slot>`标签定义slot,在使用组件时使用`<template v-slot></template>`或`<slot></slot>`标签插入内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值