美文网首页
Vue关于插槽使用的一些总结

Vue关于插槽使用的一些总结

作者: 李牧敲代码 | 来源:发表于2019-03-06 22:46 被阅读0次

先看下官网的解释:Vue 实现了一套内容分发的 API,这套 API 的设计灵感源自 Web Components 规范草案,将 <slot> 元素作为承载分发内容的出口。
内容分发的概念: 将父组件的内容放到子组件指定的位置叫做内容分发

应用场景:

简而言之,插槽就是用来进行内容分发的。

image.png

具名插槽

例子:
// 父组件的内容
<template>
    <div class="home">
        <slotcenter>
            <template v-slot:header>
                <h1>this is header</h1>
            </template>
            <template v-slot:footer>
                <h1>this is footer</h1>
            </template>
            <template v-slot:default>
                <h1>default</h1>
            </template>
        </slotcenter>
    </div>
</template>

<script>
// @ is an alias to /src
import slotcenter from "@/components/slot.vue";

export default {
    name: "home",
    components: {
        slotcenter
    }
};
</script>
//子组件的内容
<template>
    <div>
        
        <slot name="footer">footer</slot>
        <slot name="header">header</slot>
        <slot>default</slot>
    </div>
</template>
<script>
export default {
    name: "slotcenter",
    components: {}
};
</script>
<style>
</style>

以上就是具名插槽的使用。

具名插槽和独占默认插槽的缩写语法

具名插槽缩写语法的例子
<template v-slot:footer>
       <h1>this is footer</h1>
</template>
//改成下面的形式
<template #footer>
     <h1>this is footer</h1>
</template>

独占默认插槽的缩写语法

在上述情况下,当被提供的内容只有默认插槽时,组件的标签才可以被当作插槽的模板来使用。这样我们就可以把 v-slot 直接用在组件上:

<current-user v-slot="slotProps">
  {{ slotProps.user.firstName }}
</current-user>
注意默认插槽的缩写语法不能和具名插槽混用,因为它会导致作用域不明确:
<!-- 无效,会导致警告 -->
<current-user v-slot="slotProps">
  {{ slotProps.user.firstName }}
  <template v-slot:other="otherSlotProps">
    slotProps is NOT available here
  </template>
</current-user>
只要出现多个插槽,请始终为所有的插槽使用完整的基于 <template> 的语法:
<current-user>
  <template v-slot:default="slotProps">
    {{ slotProps.user.firstName }}
  </template>

  <template v-slot:other="otherSlotProps">
    ...
  </template>
</current-user>

动态插槽名

<base-layout>
  <template v-slot:[dynamicSlotName]>
    ...
  </template>
</base-layout>

注意这里的dynamicSlotName不需要加引号

后备内容

就是默认值的意思,直接写在slot标签里即可

<slot name="slot1">默认值</slot>

作用域插槽

就是父组件能访问子组件数据的插槽。

相关概念

插槽prop: 绑定在 <slot> 元素上的特性.

例子
//子组件
<template>
    <div>       
        <slot name="footer" :age="age">footer</slot>
    </div>
</template>
<script>
export default {
    name: "slotcenter",
    data() {
        return {
            age: 12
        }
    }
};
</script>
<style>
</style>
//父组件
<template>
    <div class="home">
        <slotcenter>
            <template v-slot:footer="footprops">
                <h1>{{footprops.age}}</h1>
            </template>
        </slotcenter>
    </div>
</template>

<script>
// @ is an alias to /src
import slotcenter from "@/components/slot.vue";

export default {
    name: "home",
    data() {
        return {
        }
    },
    components: {
        slotcenter
   
    }
};
</script>

【完】

以上就是关于插槽的一些总结,如果不对或者需要补充地方欢迎留言~

相关文章

  • vue插槽

    vue插槽slot的理解与使用 vue slot插槽的使用介绍及总结

  • Vue关于插槽使用的一些总结

    先看下官网的解释:Vue 实现了一套内容分发的 API,这套 API 的设计灵感源自 Web Components...

  • Vue插槽slot

    使用了那么久的vue,到现在才知道vue自带的组件插槽slot,说真的,插槽的作用在一些场景下非常有用。 插槽分为...

  • (十八)补充-Vue3中插槽的使用

    本章我们将了解到的是vue3中常用插槽的使用; vue3中的插槽是在用的时候和vue2的区别略有不同,常见插槽使用...

  • vue 插槽的使用

    vue 插槽手册 深入理解vue中的slot与slot-scope 插槽的使用其实是很简单首先要明白插槽是使用在子...

  • 2020-07-23 一次性讲明白vue插槽slot

    vue插槽slot 一、前言 vue官方文档中在"组件基础"内容中提到组件可以通过插槽分发内容,那插槽是怎么使用的...

  • vue插槽slot

    vue插槽slot 一、前言 vue官方文档中在"组件基础"内容中提到组件可以通过插槽分发内容,那插槽是怎么使用的...

  • 18、Vue3 作用域插槽

    作用域插槽:让插槽内容能够访问子组件中,vue2中作用域插槽使用slot-scope,vue3中使用v-slot ...

  • vue插槽

    vue插槽使用 1.基本插槽实现父级设置 子级设置 2、使用name设置插槽显示隐藏父组件使用v-slot绑定 子...

  • 关于使用vue的一点心得

    本人使用vue时间不长,完整的项目算是做完了2个。现在写一些关于使用vue的心得和总结吧。我发现只有总结之后,才会...

网友评论

      本文标题:Vue关于插槽使用的一些总结

      本文链接:https://www.haomeiwen.com/subject/eiazuqtx.html