vue render函数和 scopedSlots 的使用 使用插槽进行传参数

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
</head>

<body>
    <div id="app">
        <table-com>
            <template v-slot:con>
                <table-row></table-row>
            </template>
            <template v-slot:con1>
                <table-row></table-row>
            </template>
            <template v-slot:con2="{name1}">
                <table-row v-for="(item,index) in name1"></table-row>
            </template>
        </table-com>
    </div>

    <!-- 
    1.创建一个table组件 tr必须嵌套在tbody中
    2.使用render函数来进行页面节点的渲染 可以避免bug

     -->
    <!-- 
        有一个问题
        tr必须要在tbody中
     -->
    <!-- <template id="table">
        <div>
            <table border cellspacing="0" cellpadding="10" width="500">
                <tbody>
                    <slot></slot>
                </tbody>

            </table>
        </div>
    </template> -->
    <template id="row">
        <tr>
            <td>13213</td>
            <td>13213</td>
            <td>13213</td>
            <td>13213</td>
        </tr>
    </template>
    <!-- 

        插槽
        让用户自定义数据的显示格式
     -->
    <script src="../../vue.js"></script>
    <script>
        Vue.component("table-com",
            {
                data() {
                    return {
                        val: 'qweqwe',
                        arr: [
                            { id: 1, name: '刘亦菲' },
                            { id: 2, name: '赵丽颖' },
                            { id: 3, name: '秦岚' },
                            { id: 4, name: '刘涛' },
                            { id: 5, name: '王子文' }
                        ]
                    }
                },
                render(h) {
                    // 一种proxy 包含当前vue实例
                    console.log(this)
                    // 创建tag需要使用数组
                    return h('table', [h('tbody', [
                        this.$slots.con,
                        this.$slots.con1,
                        this.$scopedSlots.con2({ name1: this.arr })
                    ])])
                },
                mounted() {
                    console.log(this)
                    // 当前vue实例
                    // 使用实例中可以获取到的slot项 将其渲染在页面中
                    // console.log( this.$slots )
                },
            });
        Vue.component("table-row",
            {
                data() {
                    return {
                        val: 'qweqwe',

                    }
                }, template: "#row",
            });
        const vm = new Vue({
            data: {
                // arr: [
                //     { id: 1, name: '刘亦菲' },
                //     { id: 2, name: '赵丽颖' },
                //     { id: 3, name: '秦岚' },
                //     { id: 4, name: '刘涛' },
                //     { id: 5, name: '王子文' }
                // ]
            }
        }).$mount("#app");
        /*
        封装组件
        发表到npm上 大家都能使用
        */
        // 创建一个table组件
    //问题:tr必须嵌套在tbody中
    //this.$slots可以获取插槽内容
    //render函数中 作用域插槽的用法
    //1.如果要使用作用域插槽,那么就要用this.$scopedSlots
    //2.在render函数中 通过this.$scopedSlots.default({arr:this.arr})
    //3.在插槽内容上 使用v-slot接收参数 
    </script>
</body>

</html>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Antd 表格组件的 `customRender` 和 `scopedSlots` 是用于自定义表格单元格内容的两种不同方式。 `customRender` 是一个函数,用于自定义表格单元格的渲染内容。你可以在函数中根据入的参数来返回自定义的渲染结果。 `scopedSlots` 是一个对象,用于自定义表格单元格的内容。你可以在对象中定义不同的名称,并将其对应的内容作为表格单元格的内容。 当 `customRender` 和 `scopedSlots` 同时存在时,Antd 表格组件会优先使用 `customRender` 来渲染单元格内容。这意味着,如果你同时使用了 `customRender` 和 `scopedSlots`,`scopedSlots` 中的将会失效。 如果你想要使用 `scopedSlots` 来自定义表格单元格的内容,可以将 `customRender` 的值设为 `undefined` 或 null,这样 Antd 表格组件就会使用 `scopedSlots` 中定义的来渲染单元格内容。 以下是一个示例代码: ```vue <template> <a-table :columns="columns" :data-source="data"> <template v-slot:title> Custom Table </template> <template v-slot:name="{ text }"> <span style="color: red">{{ text }}</span> </template> </a-table> </template> <script> export default { data() { return { columns: [ { title: 'Name', dataIndex: 'name', customRender: undefined, // or null scopedSlots: { customRender: 'name' } }, { title: 'Age', dataIndex: 'age' }, { title: 'Address', dataIndex: 'address' } ], data: [ { name: 'John Doe', age: 30, address: 'New York' }, // ... ] }; } }; </script> ``` 在上面的示例中,我们将 `customRender` 的值设为 `undefined`,并在 `scopedSlots` 中使用 `name` 来自定义 `Name` 列的内容。这样,`scopedSlots` 中的就会生效,并且 `Name` 列的单元格内容会渲染为红色字体。 希望以上解答能对你有帮助!如有任何疑问,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值