vue中render函数阅读性比较好的写法推荐

 推荐写法一:

{
	title:'操作',
	align: 'center',
	render: (h ,params) =>{
		let row = params.row;
		let status = row.status;
		let that = this;

		let editButtonStyle = {
			props: {
				type: 'success',
				size: 'small',
				ghost: true
			},
			on: {
				click: () => {
					that.edit(row);
				}
			}
		};

		let editButton = h( 'Button' , editButtonStyle , '编辑' );

		let viewSourceButtonStyle = {
			props: {
				type: 'primary',
				size: 'small',
				ghost: true
			},
			style: {
				marginLeft: '20px'
			},
			on: {
				click: () => {
					that.viewSource(row);
				}
			}
		};

		let viewSourceButton = h( 'Button' , viewSourceButtonStyle , '查看' );

		if( status == 0 ){ // 正常
			return h( 'div', [ editButton , viewSourceButton ] );
		}else { // 作废,常规写法。
			return h('div', [
				h(
					'Button',{
						props: {
							type: 'success',
							size: 'small',
							ghost: true
						},
						on: {
							click: () => {
								that.edit(row);
							}
						}
					},'edit'
				),
				h(
					'Button' , {
						props: {
							type: 'primary',
							size: 'small',
							ghost: true
						},
						style: {
							marginLeft: '20px'
						},
						on: {
							click: () => {
								that.viewSource(row);
							}
						}
					} , '查看'
				)
			]);
		}
	}
}

推荐写法二:

render: (h ,params) =>{
    let that = this;
    return h('div', that.getTaskOperatingData(h,params.row));
}

/**
 * functionInfo :  render function package
 * createTime :  2019-04-16 15:30:02
 * creator: Cr
 * */
getTaskOperatingData ( h , row ) {
    let status = row.status;
    let that = this;

    let editButtonStyle = {
        props: {
            type: 'success',
            size: 'small',
            ghost: true
        },
        on: {
            click: () => {
                that.edit(row);
            }
        }
    };
    let editButton = h( 'Button' , editButtonStyle , '编辑' );

    let viewSourceButtonStyle = {
        props: {
            type: 'primary',
            size: 'small',
            ghost: true
        },
        style: {
            marginLeft: '20px'
        },
        on: {
            click: () => {
                that.viewSource(row);
            }
        }
    };
    let viewSourceButton = h( 'Button' , viewSourceButtonStyle , '查看' );

    let obsoleteButtonStyle = {
        props: {
            type: 'error',
            size: 'small',
            ghost: true
        },
        style: {
            marginLeft: '20px'
        },
        on: {
            click: () => {
                that.obsolete(row);
            }
        }
    };
    let obsoleteButtonButton = h( 'Button' , obsoleteButtonStyle , '作废' );

    let invalidButtonStyle = {
        props: {
            type: 'error',
            size: 'small',
            ghost: true,
            disabled: true
        },
        style: {
            marginLeft: '20px'
        },
    };
    let invalidButton = h( 'Button' , invalidButtonStyle , '已作废' );

    let data = [];
    if( status == 0 ){ // 正常
        data = [ editButton, viewSourceButton, obsoleteButtonButton ];
    }else { // 作废
        data = [ viewSourceButton,invalidButton ];
    }
    return data;
},

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值