Vue 3.0 中的slot及使用场景

1. 基本概念

在 Vue 中, slot 用于定义组件中的插槽位置,外部的内容会被插入到组件内部的这个位置。插槽的内容是动态的,可以根据需要进行传递和渲染。它允许开发者在组件外部传递任意内容,并在组件内部进行渲染,主要功能是提高组件的复用性和灵活性。

2. 使用方式

2.1. 默认插槽

最基本的插槽用法是默认插槽,在组件模板中定义 slot,在组件使用时传递内容。

  1. 子组件 MyComponent.vue
html 复制代码
<template>
    <div>
        <slot></slot>
    </div>
</template>
  1. 父组件
html 复制代码
<template>
    </Component>
    <p>This is some content passed to the slot.</p>
    </MyComponent>
</template>

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

export default {
    components: {
        MyComponent
    }
};
</script>

2.2. 具名插槽

有时需要在一个组件中使用多个插槽,这时可以使用命名插槽。

  1. 子组件 MyComponent.vue
html 复制代码
<template>
    <div>
        <header>
            <slot name="header"></slot>
        </header>
        <main>
            <slot></slot> <!-- 默认插槽 -->
        </main>
        <footer>
            <slot name="footer"></slot>
        </footer>
    </div>
</template>
  1. 父组件
html 复制代码
<template>
    <MyComponent>
        <template v-slot:header>
            <h1>Header Content</h1>
        </template>
        <template v-slot:footer>
            <p>Footer Content</p>
        </template>
        <p>Main Content</p>
    </MyComponent>
</template>

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

export default {
    components: {
        MyComponent
    }
};
</script>

2.3. 作用域插槽

作用域插槽允许子组件将数据传递给插槽内容的父组件,这在需要在插槽内容中使用子组件数据时非常有用。

  1. 子组件 MyComponent.vue
html 复制代码
<template>
    <div>
        <slot :user="user"></slot>
    </div>
</template>

<script>
export default {
    data() {
        return {
            user: {
                name: 'John Doe',
                age: 30
            }
        };
    }
};
</script>
  1. 父组件
javascript 复制代码
<template>
    <MyComponent>
        <template v-slot:default="slotProps">
            <p>User Name: {{ slotProps.user.name }}</p>
            <p>User Age: {{ slotProps.user.age }}</p>
        </template>
    </MyComponent>
</template>

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

export default {
    components: {
        MyComponent
    }
};
</script>

3. 使用场景

复用组件结构:插槽允许开发者在不同的上下文中复用相同的组件结构,而改变内容。比如模态框、卡片组件等。

动态内容插入:在一些布局组件中,可以通过插槽动态插入不同的内容,而不需要修改组件本身。

自定义渲染逻辑:在复杂组件中,通过作用域插槽可以将一些数据传递给父组件,从而让父组件来决定如何渲染这些数据。

嵌套组件:在多层嵌套组件中,插槽可以让外层组件将内容传递给内层组件,从而实现复杂的嵌套布局。

相关推荐
RichardLau_Cx7 天前
零依赖!纯前端 AI 辅助病例管理系统 aiCaseManage:无后端也能实现诊疗行为核验
前端·人工智能·前端开发·localstorage·医疗科技·ai辅助开发·零依赖项目
天若有情67310 天前
从 try-catch 回调到链式调用:一种更优雅的 async/await 错误处理方案
前端·异常处理·前端开发·async·异步·await·异步编程
长路 ㅤ   13 天前
优化篇之AI Chat响应换行渲染效果不生效
前端开发·markdown渲染·事件流·ai对话·换行处理
_OP_CHEN14 天前
【前端开发之JavaScript】(四)JS基础语法下篇:函数与对象核心要点深度解析
开发语言·前端·javascript·界面开发·前端开发·网页开发·语法基础
SuniaWang16 天前
Spring Boot + Spring AI + Vue 3 + TypeScript + Milvus 项目实战
java·人工智能·spring boot·spring·typescript·框架·前端开发
_OP_CHEN18 天前
【前端开发之JavaScript】(二)JS基础语法上篇:吃透变量 / 类型 / 输入输出
开发语言·javascript·html·ecmascript·前端开发·网页开发
全栈探索者20 天前
useState 换个名字叫 @State,仅此而已
react·harmonyos·arkts·前端开发·deveco studio·状态管理·鸿蒙next
全栈探索者23 天前
@Component + struct = 你的新函数组件——React 开发者的鸿蒙入门指南(第 2 期)
react·harmonyos·arkts·前端开发·deveco studio·鸿蒙next·函数组件
realhuizhu24 天前
为什么程序员配出的颜色像"斑斓的灰"?因为你还在靠直觉
前端开发·ai工具·ui设计·deepseek·程序员提升
bin91531 个月前
(文后附完整代码)html+css+javascript 弓箭射击游戏项目分析
前端·javascript·css·游戏·html·前端开发