vue中的h函数使用

hykeda2年前Vue2151

一、参数设置

h函数接收三个参数。

第一个参数:,可以为一个html标签,一个组件,一个异步组件,或者是一个函数式组件。

第二个参数:{ Object } Props,与attributes和props,以及事件对应的对象,我们可以在模板中使用,如果没有需要传入的属性,可以设置为null。

第三个参数:{String | Object |Array}可以是字符串Text文本或者是h函数构建的对象再者可以是有插槽的对象。

二、基本使用

return () => h('div', {class: 'demo'}, 'hello')

上面会返回一个div标签,<div class="demo">hello</div>。

如果要添加点击事件:

return () => h('div', {class: 'demo',onClick: closeDiv}, 'hello')

很多的属性都可以加到第二个参数中去。

如果需要多个标签或者组件嵌套:

return () => h('div', {class: 'demo'}, [
                h(Space, {}, [
                    h(Button, {size: 'small',onClick: closeDivCancel}, '取消'),
                    h(Button, {size: 'small', type: 'primary', onClick:delFunction(item,index)}, '确定'),
                    h(ABC, {}, {
                        default:props=>h('h3',{},‘你好’),// 默认插槽
                        test:props=>h('h3',{},‘test’),// 叫test的插槽
                    })
                ]),
            ])

这段代码创建了一个div,div中引入了一个space组件,组件中引入了两个button组件。组件分给配置了点击事件,大小等。用第三个参数传入h函数数组的方式实现。


arco design 框架中 Model.warning设置了cancelText无效,只能用footer插槽来实现,结合h函数:

delDiv.value = Modal.warning({
        title: '温馨提示',
        content: '您确定要删除该数据吗?',
        okText: '确定',
        cancelText: '取消',
        footer:(
            () => h('div', {class: 'info-modal-content'}, [
                h(Space, {}, [
                    h(Button, {size: 'small',onClick: closeDivCancel}, '取消'),
                    h(Button, {size: 'small', type: 'primary', onClick:delFunction(item,index)}, '确定')
                ]),
            ])
        )
    });


标签: vueh函数

相关文章

Vue3二维码生成

一、装上插件npm install --save qrcode.vue二,引入插件到页面中import QrcodeVue from ...

uni-app中使用iconfont字体,亲测有用

一、下载好iconfont文件,复制iconfont.ttf这个文件至项目。 在你的项目中创建一个iconfont.css文件,文件内容: @font-face { font-fam...

vite+vue 插件/组件集合

自动引入插件:https://github.com/antfu/unplugin-auto-importvue的按需组件自动导入:https://github.com/antfu/unplugin-v...

前端常用的UI组件库

Arco DesignTaro UISemi DesignAnt DesignElementView DesignVantLayui-vueFusion Design1. Arco Desi...

在html5中使用Vue3,并且使用组件形式

有的时候为了使用vue编写一些前端程序,如果直接搭建框架感觉太过庞大了。如何直接在html中使用呢?接下来详细介绍:1)引入Vue,并创建Vue实例在官网上,已经讲得很清楚了,我们可以这样使用&nbs...

直接在浏览器中使用 vue3+element-plus中icons的方法

首先引入文件,如果用cdn的方式:<script src="//unpkg.com/@element-plus/icons-vue"></script&...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。