插槽

作者: 简单tao的简单 | 来源:发表于2024-01-22 09:52 被阅读0次

具名插槽

// 父组件

<script setup>
import BaseLayout from './BaseLayout.vue'
</script>

<template>
  <BaseLayout>
    <template #header>
      <h1>Here might be a page title</h1>
    </template>

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

    <template #footer>
      <p>Here's some contact info</p>
    </template>
  </BaseLayout>
</template>
// 子组件

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

<style>
footer {
    border-top: 1px solid #ccc;
    color: #666;
    font-size: 0.8em;
}
</style>

作用域插槽

让插槽内容能够访问子组件中的数据

// 父组件

<script setup>
import MyComponent from './MyComponent.vue'
</script>

<template>
    <MyComponent v-slot="slotProps">
       {{ slotProps.text }} {{ slotProps.count }}
    </MyComponent>
</template>
// 子组件

<script setup>
const greetingMessage = 'hello'
</script>

<template>
   <div>
      <slot :text="greetingMessage" :count="1"></slot>
   </div>
</template>

相关文章

  • vue----slot插槽

    插槽分类 匿名插槽 具名插槽 作用域插槽

  • vue中slot插槽的使用

    插槽的种类:1、默认插槽、具名插槽、作用域插槽、解构插槽、动态插槽几种。转原文:https://www.jians...

  • vue3中的插槽

    插槽 默认插槽 具名插槽,v-slot可以简写为# 动态插槽 #[dynamicSlotName] 作用域插槽(...

  • 2.插槽

    匿名插槽 具名插槽 作用域插槽

  • 深入理解vue中的slot与slot-scope(自 2.6.0

    单个插槽 | 默认插槽 | 匿名插槽首先是单个插槽,单个插槽是vue的官方叫法,但是其实也可以叫它默认插槽,或者与...

  • vue 插槽 slot

    插槽使用 普通插槽 具名插槽 使用具名插槽 从插槽里面传值出来如何接收? 如: 如何判断某个插槽是否被使用 组件内...

  • vue插槽

    默认插槽(没有名字的插槽) 具名插槽(带名字的插槽) 老版 2.6.0以前 新版 作用域插槽(父组件可以获取子组件...

  • Vue总结4-插槽,Vuex,VueRouter

    1.插槽 1.1匿名插槽 52-Vue组件-匿名插槽 ...

  • 组件化高级

    插槽 什么是插槽 生活中有很多地方都有用到插槽,比如电脑的USB插槽,插板上的电源插槽,目的是让我们原来的设备具备...

  • vue 插槽slot

    插槽的定义: 插槽的使用:

网友评论

      本文标题:插槽

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