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. 使用场景

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

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

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

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

相关推荐
海市公约2 天前
Axios网络请求库核心特性与拦截器封装实践
axios·前端开发·promise·拦截器·网络封装
冴羽yayujs5 天前
前端周报:Google I/O 发布 Agentic Web、TypeScript 6.0 正式版、npm 安全新策略
前端·javascript·前端开发·前端学习·前端周报
冴羽yayujs9 天前
快速夯实 JavaScrilpt 基础的 33 个概念
前端·javascript·github·前端开发
冴羽yayujs12 天前
前端周报:Rolldown 1.0 正式发布、TanStack 遭遇史诗级供应链攻击、Bun 全面迁移至 Rust
前端·rust·前端开发·前端周报
极客小俊22 天前
【从零到一】用HTML5+CSS+JavaScript实现一个属于自己的mp3免费音乐播放器 (4) JS交互功能(音乐进度条)
javascript·css·html5·前端开发·免费教程·代码案例·手搓音乐播放器
不会写程序的未来程序员1 个月前
nvm 安装教程:Node.js 版本管理全攻略 (Win/Mac/Linux) + .nvmrc 实战
linux·macos·node.js·前端开发·环境配置·nvm
神州数码云基地1 个月前
AI助手语音交互:从技术到体验
人工智能·ai·语音识别·前端开发·tts·ai语音
是席木木啊2 个月前
前端接口熔断:概念、场景、自定义封装及企业级库对比
性能优化·前端开发·接口熔断
尘中客2 个月前
放弃 Echarts?前端直接渲染后端高精度 SVG 矢量图流的踩坑记录
前端·javascript·echarts·前端开发·svg矢量图·echarts避坑
烛衔溟2 个月前
TypeScript 特殊类型与空值安全
安全·typescript·前端开发·空值处理