vue3 - 13.插槽全家桶

本文深入探讨了Vue3中的插槽特性,包括匿名插槽、具名插槽、插槽作用域以及动态插槽的使用方法。通过实例展示了如何在父组件中自定义子组件的内容,增强组件的复用性和灵活性。
摘要由CSDN通过智能技术生成

vue3 - slot 插槽:插槽的作用是比如我们声明一个公共组件,在每个模块中引用公共组件的时候,可能某些模块的内容不一样,为了可以在单页面中方便自定义想要的内容。

1. slot匿名插槽:

我们声明一个子组件,在父组件中引用

index 父组件:

<template>
  <div>
    <Card>
      <template v-slot>
          匿名插槽插入成功
      </template>
    </Card>
  </div>
</template>
<script setup lang='ts'>
  import { ref } from 'vue'
  import Card from './components/Card.vue'
</script>
<style scoped>
</style>

Card 子组件:

<template>
    <div>
        <header></header>
        <section>
            <div>
                <slot></slot>
            </div>
        </section>
        <footer></footer>
    </div>
</template>
<script setup lang='ts'>
    import { ref } from 'vue'
</script>
<style scoped>
</style>

2. slot具名插槽:

index 父组件:

<template>
  <div>
    <Card>
      <template v-slot:header>
        <div>头部header内容</div>
      </template>
      <template v-slot:content>
        <div>中部content内容</d
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WARN "css.modules" option in vue.config.js is deprecated now, please use "css.requireModuleExtension" instead. INFO Starting development server... 98% after emitting CopyPlugin WARNING Compiled with 17 warnings 09:43:57 warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'computed' was not found in 'vue' warning in ./src/router/index.js "export 'default' (imported as 'VueRouter') was not found in 'vue-router' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'defineComponent' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'getCurrentInstance' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'h' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'inject' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'nextTick' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'onActivated' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'onDeactivated' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'onUnmounted' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'provide' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'reactive' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'ref' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'shallowRef' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'unref' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'watch' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'watchEffect' was not found in 'vue'这个报错因为什么
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值