VUE3 入门教程 Vue 3.0 函数式组件

2024-02-25 开发教程 VUE3 入门教程 匿名 0

#概览

就变化而言,属于高等级内容:

  • 在 3.x 中,函数式组件 2.x 的性能提升可以忽略不计,因此我们建议只使用有状态的组件
  • 函数式组件只能使用接收 propscontext的普通函数创建 (即:slotsattrsemit)。
  • 非兼容变更:functionalattribute 在单文件组件 (SFC) <template>已被移除
  • 非兼容变更:{ functional: true }选项在通过函数创建组件已被移除

更多信息,请继续阅读!

#介绍

在 Vue 2 中,函数式组件有两个主要用例:

  • 作为性能优化,因为它们的初始化速度比有状态组件快得多
  • 返回多个根节点

然而,在 Vue 3 中,有状态组件的性能已经提高到可以忽略不计的程度。此外,有状态组件现在还包括返回多个根节点的能力。

因此,函数式组件剩下的唯一用例就是简单组件,比如创建动态标题的组件。否则,建议你像平常一样使用有状态组件。

#2.x 语法

使用 <dynamic-heading>组件,负责提供适当的标题 (即:h1h2h3,等等),在 2.x 中,这可能是作为单个文件组件编写的:

// Vue 2 函数式组件示例
export default {
functional: true,
props: ['level'],
render(h, { props, data, children }) {
return h(`h${props.level}`, data, children)
}
}

或者,对于喜欢在单个文件组件中使用 <template>的用户:

// Vue 2 函数式组件示例使用 <template>
<template functional>
<component
:is="`h${props.level}`"
v-bind="attrs"
v-on="listeners"
/>
</template>
<script>
export default {
props: ['level']
}
</script>

#3.x 语法

#通过函数创建组件

现在在 Vue 3 中,所有的函数式组件都是用普通函数创建的,换句话说,不需要定义 { functional: true }组件选项。

他们将接收两个参数:propscontextcontext参数是一个对象,包含组件的 attrsslots,和 emitproperty。

此外,现在不是在 render函数中隐式提供 h,而是全局导入 h

使用前面提到的 <dynamic-heading>组件的示例,下面是它现在的样子。

import { h } from 'vue'
const DynamicHeading = (props, context) => {
return h(`h${props.level}`, context.attrs, context.slots)
}
DynamicHeading.props = ['level']
export default DynamicHeading

#单文件组件 (SFC)

在 3.x 中,有状态组件和函数式组件之间的性能差异已经大大减少,并且在大多数用例中是微不足道的。因此,在 SFCs 上使用 functional的开发人员的迁移路径是删除该 attribute,并将 props的所有引用重命名为 $props,将 attrs重命名为 $attrs

使用之前的 <dynamic-heading>示例,下面是它现在的样子。

<template>
<component
v-bind:is="`h${$props.level}`"
v-bind="$attrs"
/>
</template>
<script>
export default {
props: ['level']
}
</script>

主要的区别在于:

  1. functionalattribute 在 <template>中移除
  2. listeners现在作为 $attrs的一部分传递,可以将其删除

#下一步

有关新函数式组件的用法和对渲染函数的更改的详细信息,见:

  • 迁移:渲染函数
  • 指南:渲染函数