news 2026/4/23 16:38:57

vue3使用h函数如何封装组件和$attrs和props的区别

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
vue3使用h函数如何封装组件和$attrs和props的区别

,插槽如何穿透

3,暴露实例以及实例中的方法

在vue3中的$attrs的变化

vue3中$listeners已被删除合并到$attrs中。

vue3的$attrs现在包括class和style属性。

vue2中不包含class和style属性。

也就是说:当子组件写上 v-bind="$attrs"

父组件就可以使用子组件的内置事件和内置属性了。

下面我们会详细说一下$attrs

props 如何进行传递属性和事件

我们可以在子组件中使用 v-bind="$attrs"

这样可以把父组件中的属性传递给子组件了

// 子组件

<template>

<div>

<!-- v-bind="$attrs" 可以接收到父组件中的属性设置 -->

<el-input v-bind="$attrs"></el-input>

</div>

</template>

// 父组件

<template>

<div>

<MyInput class="set-width" placeholder="请输入名称" clearable v-model="name" @blur="clearHandler"></MyInput>

</div>

</template>

<script setup lang="ts">

import MyInput from '@/components/MyInput.vue'

import { ref } from 'vue';

let name = ref('')

const clearHandler = () => {

console.log('失去焦点啦')

name.value += 'copy'

}

</script>

<style lang="scss" scoped>

.set-width {

margin: 100px;

width: 300px;

}

</style>

01

01-1

如何解决写组件时没有属性提示的问题

我们发现一个问题:在父组件中的组件写相关属性时,没有属性提示。

// 子组件

<template>

<div>

<!-- v-bind="props" 现在我们的属性肯定是 element-plus 的内置属性了 -->

<el-input v-bind="props"></el-input>

</div>

</template>

<script setup lang="ts">

// 引入 input 的所有属性

import { type InputProps} from 'element-plus'

// 定义 props, Partial将必填属性变成可选属性

const props = defineProps<Partial<InputProps>>()

</script>

这样父组件在使用的时候,就可以看到属性提示了。

插槽如何封装1: 通过 template 来封装插槽

<template>

<div>

<el-input v-bind="props">

<!-- 插槽 -->

<template v-for="(_, slotName) in $slots" #[slotName]>

<slot :name="slotName"></slot>

</template>

</el-input>

</div>

</template>

<script setup lang="ts">

// 引入 input 的所有属性

import { type InputProps} from 'element-plus'

// 定义 props, Partial将必填属性变成可选属性

const props = defineProps<Partial<InputProps>>()

插槽如何封装2: 通过h函数来处理插槽

我们使用h函数来进行封装。

h函数如果第1个参数如果是组件,那么第三个参数就是插槽

<template>

<div>

<!-- 我们使用h函数来进行封装,h函数如果第1个参数如果是组件,那么第三个参数就是插槽 -->

<component :is="h(ElInput, {...$attrs,...props}, $slots)"></component>

</div>

</template>

<script setup lang="ts">

import { h } from 'vue'

// 引入 input 的所有属性

import { type InputProps, ElInput} from 'element-plus'

// 定义 props, Partial将必填属性变成可选属性

const props = defineProps<Partial<InputProps>>()

</script>

// 父组件

<template>

<div>

<MyInput class="set-width" placeholder="请q输入内容">

<!-- 在组件中使用插槽 -->

<template #prepend>

<el-select v-model="select" placeholder="Select" style="width: 115px">

<el-option label="Restaurant" value="1" />

<el-option label="Order No." value="2" />

<el-option label="Tel" value="3" />

</el-select>

</template>

<template #append>.com</template>

</MyInput>

</div>

</template>

<script setup lang="ts">

import MyInput from '@/components/MyInput.vue'

import { ref } from 'vue';

const select = ref('1')

</script>

<style lang="scss" scoped>

.set-width {

margin: 100px;

width: 300px;

}

</style>

02

暴露实例以及实例中的方法

我们可以通过 defineExpose 来暴露实例以及方法【常用的】

也可以通过vm.exposed来进行暴露实例以及方法

需要注意组件最初设置了v-if=false这种情况

// 子组件

<template>

<div>

<!-- 我们使用h函数来进行封装,h函数如果第1个参数如果是组件,那么第三个参数就是插槽 -->

<component :is="h(ElInput, {...$attrs,...props, ref: nodeRef}, $slots)"></component>

</div>

</template>

<script setup lang="ts">

import { h, getCurrentInstance } from 'vue'

// 引入 input 的所有属性

import { type InputProps, ElInput} from 'element-plus'

// 定义 props, Partial将必填属性变成可选属性

const props = defineProps<Partial<InputProps>>()

// 获取当前组件实例

const vm = getCurrentInstance()

// ref可以是一个字符串,也可以是一个函数。这样父组件就可以通过ref访问这个组件的实例了

function nodeRef(inputInstance) {

// 现在我们把子组件实例给他,当组件使用了v-if=false的时候,inputInstance为null

// 这里我们是把实例(实例中包含方法)暴露出去

vm.exposed= inputInstance || {}

// 代理对象也要做同步的更改

vm.exposeProxy = inputInstance || {}

}

</script>

// 父组件

<template>

<div>

<MyInput class="set-width" v-model="msg" ref="NodeInputRef" placeholder="请输入内容" @blur="clearHandler">

<!-- 在组件中使用插槽 -->

<template #prepend>

<el-select v-model="select" placeholder="Select" style="width: 115px">

<el-option label="Restaurant" value="1" />

<el-option label="Order No." value="2" />

<el-option label="Tel" value="3" />

</el-select>

</template>

<template #append>.com</template>

</MyInput>

<el-button @click="getHandler">清空值</el-button>

</div>

</template>

<script setup lang="ts">

import MyInput from '@/components/MyInput.vue'

import { ref } from 'vue';

const select = ref('1')

const msg = ref('放假快乐')

const NodeInputRef = ref(null)

// 获取实例中的方法

const getHandler = () => {

NodeInputRef.value?.clear()

}

const clearHandler = () => {

console.log('失去焦点啦')

}

</script>

03

04

另外一种暴露方式

常见的暴露方式

defineProps({

name:xxx,

age:xxx,

})

等价与下面这一种

vm.exposed= {

name:xxx,

age:xxx,

}

vue3 中的 props

props 是组件的自定义属性,用于从父组件向子组件传递数据。

props 不会包含继承的属性(如 class 和 style),除非显式声明。

vue3 中的 $attrs

vu3中$attrs: 包含了所有[传递]给[子组件]的非 props 属性。如:继承的属性(如 class 和 style)以及未在 props 中声明的属性。

vue3中的$attrs: 包含 style和class。$attrs包含着数据和事件。

vue3 $listeners已被删除合并到$attrs中。

在vue2中的$attrs

vu2中$attrs: 包含了所有[传递]给[子组件]的非 props 属性和style和class之外的属性。

vue2中的$attrs: 不包含 style和class

下面是详细的讲解:

在V ue2 中,attrs里面包含着上层组件传递的所有数据(除style和class)

当一个组件声明了prop时候,attrs里面包含除去prop里面的数据剩下的数据。

结合inheritAttrs:false,可以将传递下来的数据应用于其他元素,而不是根元素。

h函数封装上面的组件

有些的小伙伴说:我们是否可以使用h函数去封装上面的组件呢?

<script lang="ts">

import { defineComponent, h, getCurrentInstance } from 'vue'

import { type InputProps, ElInput } from 'element-plus'

export default {

// 组件名称

name: 'MyInput',

inheritAttrs: false,

setup(props, { attrs, slots }) {

console.log('attrs', attrs)

// attrs:除去props中声明的属性。包含属性和事件

const vm = getCurrentInstance()

function nodeRef(inputInstance: any) {

vm.exposed = inputInstance || {}

vm.exposeProxy = inputInstance || {}

}

return () => h(ElInput, {

...attrs,

...props,

ref: nodeRef

}, slots)

}

}

<template>

<div>

<MyInput class="set-width" placeholder="请输入名称" clearable v-model="name" @blur="clearHandler"></MyInput>

</div>

</template>

<script setup lang="ts">

import MyInput from '@/components/MyInput.vue'

import { ref } from 'vue';

let name = ref('')

const clearHandler = () => {

console.log('失去焦点啦')

name.value += 'copy'

}

</script>

<style lang="scss" scoped>

.set-width {

margin: 100px;

width: 300px;

}

</style>

05

遇见问题,这是你成长的机会,如果你能够解决,这就是收获。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 10:17:25

用 Go 像写 Web 一样做桌面应用:完全离线的手机号归属地查询工具

前阵子我做了一个小工具&#xff1a;一个完全离线的手机号归属地查询桌面应用 功能本身其实并不复杂&#xff0c;但在这个过程中&#xff0c;我反而重新认识了一次 用 Go 做桌面应用&#xff0c;其实可以非常像在写一个 Web 项目。 这篇文章不打算讲手机号归属地怎么查&…

作者头像 李华
网站建设 2026/4/22 23:24:59

【工具变量】地市链长制DID数据集(2016-2025年)

一、数据简介 本指标数据为地级市级数据&#xff0c;通过对全国地级市相关政策文件、政府公开信息等进行系统梳理与整理获得&#xff0c;未借助第三方商业数据库。数据时间跨度为2016-2025年&#xff0c;最终共获得3370条有效数据&#xff0c;其中链长制虚拟变量&#xff08;c…

作者头像 李华
网站建设 2026/4/22 17:01:32

小型工厂生产管理系统推荐:2025中小制造企业MES系统权威排行榜

本文整理2025中小制造企业MES系统排行榜&#xff0c;基于多维度权威评估&#xff0c;帮制造企业老板、生产总监、采购负责人等决策人群快速锁定高适配、高性价比方案&#xff0c;解决“不知如何判断系统适配性”“担心成本超支”“怕落地效果差”等选型核心痛点。一、权威评估维…

作者头像 李华
网站建设 2026/4/23 10:19:33

在线快速制作伴奏软件功能AI编曲软件伴奏功能

解锁 AI 音乐新玩法&#xff1a;在线快速制作伴奏神器大揭秘 在音乐创作的道路上&#xff0c;许多人常常面临着各种难题。对于初学者来说&#xff0c;想要快速制作出一个满意的伴奏简直比登天还难&#xff0c;专业的编曲软件操作复杂&#xff0c;学习成本高&#xff0c;让人望而…

作者头像 李华
网站建设 2026/4/23 10:19:32

基于PLC的智能楼宇控制系统设计

第二章 智能楼宇系统总体方案确定 2.1 智能楼宇系统总体方案的设计 2.1.1 智能楼宇概述 世界各国对智能楼宇地定义参差不齐。在日本的智能楼宇的提法跟我们的国家相类似&#xff0c;他们定义智能建筑&#xff1a;集成了计算机&#xff0c;通讯等多方面先进的技术来实现照明、报…

作者头像 李华
网站建设 2026/4/23 11:52:57

人工智能训练效率革命:Modded-NanoGPT的技术突破与实践路径

在人工智能训练成本日益攀升的背景下&#xff0c;传统模型训练面临着能效比低、训练周期长的双重挑战。Modded-NanoGPT项目通过算法创新与系统优化&#xff0c;实现了GPT-2&#xff08;124M参数&#xff09;级别模型训练时间从45分钟压缩至2.86分钟的突破性进展&#xff0c;同时…

作者头像 李华