vue3 h函数的使用

使用说明

h函数返回一个虚拟节点即虚拟DOM

render() {
  return h('h1', {}, 'Some title')
}

接收 type,props,children 三个参数

  • type:String | Object | Function (必填项)
    String:即html标签,‘button’,‘span’
    Object:组件(比如使用第三方UI库)
    Function:函数式组件

  • props:Object
    在函数式组件中,props又被分为Props和Context,组件中声明props值,才会被放到Props对象中,否则全部被放到context中, context又包括attr 标签属性,emits,slot

  • children:String | Array | Object

实践

import { h } from "vue";
const DynamicTemplate = (props, context) => {
  return h(
    props.label,
    { ...context.attrs.config, ...context.attrs.event },
    context.slots
  );
};

DynamicTemplate.props = ["label"];

export default DynamicTemplate;
<template>
  <DynamicTemplate
    :label="i.template.label"
    :config="i.template.attrConfig"
    :event="i.template.event(scope)"
  >
    {{ i.template.text }}
 </DynamicTemplate>
 </template>
import { ElButton } from "element-plus";
{
	template: {
      label: ElButton,
      text: "查看",
      attrConfig: {
        type: "text",
        disabled: true,
      },
      event(params) {
        return {
          onClick($event) {
            console.log($event);
            console.log(params);
          },
        };
      },
    }
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值