render 函数基础 | Slot与scopedSlots使用

1 篇文章 0 订阅
1 篇文章 0 订阅

什么是render 函数

render 函数需要你将模板定义为 JavaScript 对象,即用js来构建DOM

原理:

  1. 通过js对象模型所表达的html结构转换成AST(抽象语法树)用于构建虚拟节点VNode
  2. render函数再在这个虚拟节点上渲染数据,完成数据渲染后就添加到html文档中渲染到页面

优点

  1. 使模板更接近编译器,并允许你使用完整的 JavaScript 功能,而不是指令提供的子集

注意项:

  1. 优先级:render> template > el
  2. 所以Vue 选项中的 render 函数若存在,则 Vue 构造函数不会从 template 选项或通过 el 选项指定的挂载元素中提取出的 HTML 模板编译渲染函数

createElement 参数

Vue中的Render函数中有一个参数,这个参数是一个函数通常我们叫做createElement。Render函数将createElement的返回值放到了HTML中;createElement这个函数中有3个参数:

  1. 第一个参数可以是HTML标签名,组件或者函数都可以(必须的)
  2. 第二个参数为数据对象
  3. 第三个参数为子节点(字符串或数组)
    在这里插入图片描述

参考官网:(https://cn.vuejs.org/v2/guide/render-function.html#createElement-%E5%8F%82%E6%95%B0)

下面写几个例子:

1.事件的使用

//template use
<evn list="hahahah"></evn>
//contentTheTemplate
Vue.component('evn', {
            props: ['list'],
            render(h){
            //因为官方有写将 h 作为 createElement 的别名是 Vue 生态系统中的一个通用惯例,
            //实际上也是 JSX 所要求的
                let _this = this;
                return h('button', {
                    on:{
                    	//此处如果这样写,会导致页面渲染完直接触发
                    	//并不受click控制
                        // click:_this.alert(_this.list)
                        click: () => {_this.alert(_this.list)}
                    }
                }, [h('p', '大哥快点我呀')])
            }
        })
//a method is call    
     Vue.mixin({
         methods: {
             alert(res){
                 alert(res)
             },
         },
     })

2.插槽的使用

slot
	<c-list cti="[{'tit': '我在这'},{'tit': '不我在这儿'}]">
      <p>12312</p>
    </c-list>
    
	Vue.component('c-list',{
            props: ['cti'],
            render(h) {
            //使用map方法实现v-for效果
            //this.$slot 类型:{[name: string]: ?Array<VNode>}
            //this.$slots.default 为默认插槽
            //也可以定义具名插槽 this.$slots.名称 | 使用 v-slot:名称 插入
                return h('div',[this.cti.map(res=> h('div', res.tit)), this.$slots.default]
                )
            }
        })
        //上式等于
        <div v-for="item in [{'tit': '我在这'},{'tit': '不我在这儿'}]">
        	<div>{{ item.tit }}</div>
        	<slot></slot>
		</div>

结果:
在这里插入图片描述

scopedSlots

scopedSlots主要用于子组件的作用域插槽

//接受子组件数据
	<ming>
      <div slot-scope="props">{{ props.text }}</div>
      <template slot="name1" scope="props">
        <span>{{props.text}}</span>
      </template>
    </ming>
	
	Vue.component('ming', {
            render(h){
            //this.$scopedSlots 类型{[name: string]: props => Array<VNode> | undefined}
            //组件向插槽内传递参数 {'text':'caomingrui'}
                return h('div', [this.$scopedSlots.default({
                    text: 'caomingrui'
                }),
                this.$scopedSlots.name1({
                        text:'hello scope'
                    })
				])
            }
        })

结果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200731194545523.png

	<rui></rui>
	//定义子组件
	Vue.component ("c-node", {
            render(h){
                return h('div', [
                	//定义向默认操作传值
                    // this.$scopedSlots.default({
                    //     text: '000000000000'
                    // }),
                    this.$scopedSlots.ddg({
                        text: 'caomingrui123'
                    })
                ])
            }
        })
        Vue.component('rui', {
            render(h){
                return h('div',[h('c-node', {
                //接受对应值
                    scopedSlots: {
                        default: props => h('span', props.text)
                    },
                }),
                h('c-node', {
                    scopedSlots: {
                        ddg: props => h('span', props.text)
                    },
                })])
            }
        })
        

结果:
在这里插入图片描述

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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` 列的单元格内容会渲染为红色字体。 希望以上解答能对你有帮助!如有任何疑问,请随时提问。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值