joken-前端工程师

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::
  404 随笔 :: 39 文章 :: 8 评论 :: 20万 阅读

tsx 实现slot插槽

  • 父组件
<template>
    <div class="component-name">
        <child>
            <template #default="scope">
                <div>default</div>
                <div>{{ scope.a }}</div>
            </template>
            <template #myself>
                <div>sdfsdfsf</div>
            </template>
        </child>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onMounted, nextTick } from 'vue';
import child from './components/child';
</script>

<style lang="scss" scoped></style>

  • 子组件
import { defineComponent, PropType, h, computed, ref, watch } from 'vue';
import type { Reactive, Ref } from 'vue';
import styles from '../scss/child.module.scss';
import '../scss/child.scss'; //这引入的样式是全局的,会影响其他页面其他组件

export default defineComponent({
    setup(props, { slots }) {
        console.log(slots, 'slots');
        return () => {
            return (
                <>
                    {/* slots 直接访问模板函数,并且可以传入scope变量作用给模板 */}
                    <div class={styles.child}>{slots.default?.({ a: '23234' })}</div>
                    <div class={styles.child}>{slots.myself?.()}</div>
                </>
            );
        };
    }
});

posted on   joken1310  阅读(122)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示