美文网首页
2023-05-24 vue插槽(slot)的模板与JSX写法

2023-05-24 vue插槽(slot)的模板与JSX写法

作者: 追寻1989 | 来源:发表于2023-05-23 13:59 被阅读0次

vue官网API:

插槽:https://cn.vuejs.org/v2/guide/components-slots.html

JSX:https://cn.vuejs.org/v2/guide/render-function.html

说明:vue版本2.6.0以上语法

一、插槽模板传值

子组件:child.vue

<template>
    <div>
        <!-- 默认插槽 -->
        <slot :info="info"></slot>
        <!-- other插槽 -->
        <slot name="other" :info="info2"></slot>
    </div>
</template>
 
<script>
export default {
    data() {
        return {
            info: {
                title: "标题一"
            },
            info2: {
                title: "标题二"
            }
        };
    }
};
</script>

父组件:parent.vue

<child>
    <template v-slot:default="slotProps">
        <div>
            {{ slotProps.info.title }}
        </div>
    </template>
    <template v-slot:other="slotProps">
        <div>
            {{ slotProps.info.title }}
        </div>
    </template>
</child>

结果:

二、插槽传值JSX写法

子组件:child.jsx

export default {
    data() {
        return {
            info: {
                title: "标题一"
            },
            info2: {
                title: "标题二"
            }
        };
    },
    render() {
        return (
            <div>
                {this.$scopedSlots.default({
                    info: this.info
                })}
 
                {this.$scopedSlots.other({
                    info: this.info2
                })}
            </div>
        );
    }
};

父组件:parent.jsx

<child
    scopedSlots={{
        default: props => {
            return (
                <div style="line-height: 30px;">
                    {props.info.title}
                </div>
            );
        },
        other: props => {
            return (
                <div style="line-height: 30px;">
                    {props.info.title}
                </div>
            );
        }
    }}
/>

结果:

相关文章

  • vue插槽

    vue插槽slot的理解与使用 vue slot插槽的使用介绍及总结

  • slot是什么?有什么作用?原理是什么?

    slot又名插槽,是Vue的内容分发机制,组件内部的模板引擎使用slot元素作为承载分发内容的出口。插槽slot是...

  • slot(插槽)

    slot又称插槽,是Vue的内容分发机制,组件内部的模板引擎使用slot元素作为承载分发内容的出口。插槽slot是...

  • Vue之深入理解插槽—slot, slot-scope, v-s

    Vue 2.6.0 以前Vue 2.6.0 以后具名插槽 slot具名插槽 v-slot作用域插槽 slot-sc...

  • Vue学习笔记-插槽

    深入理解vue中的slot与slot-scope 插槽,也就是slot,是组件的一块HTML模板。 对于任何一个组...

  • vue 插槽的使用

    vue 插槽手册 深入理解vue中的slot与slot-scope 插槽的使用其实是很简单首先要明白插槽是使用在子...

  • vue中的slot(插槽)

    vue中的插槽————slot 什么是插槽? 插槽(Slot)是Vue提出来的一个概念,正如名字一样,插槽用于决定...

  • vue jsx使用插槽

    默认插槽:jsx: 具名插槽: App.vue

  • slot-scope到底是什么

    插槽slot 插槽,也就是slot 插槽是组件的一块HTML模板 插槽就是要将父组件中的内容渲染到子组件中。就好像...

  • 18、Vue3 作用域插槽

    作用域插槽:让插槽内容能够访问子组件中,vue2中作用域插槽使用slot-scope,vue3中使用v-slot ...

网友评论

      本文标题:2023-05-24 vue插槽(slot)的模板与JSX写法

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