详解vue3中组件的非兼容变更

作者:月岛蘑菇 时间:2024-04-28 09:23:04 

目录
  • 函数式组件

  • 异步组件的写法与defineAsyncComponent方法

  • 组件事件需要在emits选项中声明

函数式组件

functional attribute 在单文件组件 (SFC) <template> 已被移除
{ functional: true } 选项在通过函数创建组件已被移除


// 使用 <dynamic-heading> 组件,负责提供适当的标题 (即:h1,h2,h3,等等),在 2.x 中,这可能是作为单个文件组件编写的:
// Vue 2 函数式组件示例
export default {
functional: true,
props: ['level'],
render(h, { props, data, children }) {
return h(`h${props.level}`, data, children)
}
}

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

<script>
export default {
props: ['level']
}
</script>

现在在 Vue 3 中,所有的函数式组件都是用普通函数创建的,换句话说,不需要定义 { functional: true } 组件选项。
他们将接收两个参数:props 和 context。context 参数是一个对象,包含组件的 attrs,slots,和 emit property。
此外,现在不是在 render 函数中隐式提供 h,而是全局导入 h。
使用前面提到的 <dynamic-heading> 组件的示例,下面是它现在的样子。


// vue3.0
import { h } from 'vue'
const DynamicHeading = (props, context) => {
return h(`h${props.level}`, context.attrs, context.slots)
}
DynamicHeading.props = ['level']
export default DynamicHeading
// vue3.0单文件写法
<template>
<component
v-bind:is="`h${$props.level}`"
v-bind="$attrs"
/>
</template>

<script>
export default {
props: ['level']
}
</script>

主要区别在于


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

异步组件的写法与defineAsyncComponent方法

现在使用defineAsyncComponent助手方法,用于显示的定义异步组件
component选项重命名为loader
Loader函数本身不再接受resolve和rejuct参数,必须返回一个Promise


// vue2.x
// 以前异步组件是通过将组件定义为返回Promise的函数来创建
const asyncPage = () => import('./NextPage.vue')
// 或者以选项方式创建
const asyncPage = {
component: () => import('./NextPage.vue'),
delay: 200,
timeout: 3000,
error: ErrorComponent,
loading: LoadingComponent
}

// vue3.x
在vue3.x中,需要使用defineAsyncComponent来定义
import{ defineAsyncComponent } from 'vue'
import ErrorComponent from './components/ErrorComponent.vue'
import LoadingComponent from './components/LoadingComponent.vue'

// 不带选项的定义方法
const asyncPage = defineAsyncComponent(() => import('./NextPage.vue'))

// 带选项的异步组件
constasyncPageWithOptions = defineAsyncCopmonent({
loader: () => import('./NextPage.vue'),
delay: 200,
timeout: 3000,
errorComponent: ErrorComponent,
LoadingComponent: LoadingComponent
})

loader函数不再接收resolve和reject参数,且必须始终返回Promise


// vue2.x
const oldAsyncComponent = (resolve, reject) => {}
// vue3.x
const asyncComponent = defineAsyncComponent(() => new Promise((resolve, reject) => {}))

组件事件需要在emits选项中声明

vue3中现在提供了一个emits选项,类似props选项
此选项可以用于定义组件向其父对象发出的事件


<!-- vue2.x -->
<template>
<div>
<p>{{ text }}</p>
<button v-on:click="$emit('accepted')">OK</button>
</div>
</template>
<script>
export default {
props: ['text']
}
</script>

<!-- vue3.x -->
<!-- 现在和prop类似,可以用emits来定义组件发出的事件 -->
<!-- 这个选项还接收已给对象,用来向props一样对传递的参数进行验证 -->
<!-- 强烈建议记录下每个组件发出的所有emits,因为去掉了.native修饰符,未使用声明的事件的所有 * 都将包含在组建的$attr中,默认情况下,该 * 将绑定到组件的根节点 -->
<template>
<div>
<p>{{ text }}</p>
<button v-on:click="$emit('accepted')">OK</button>
</div>
</template>
<script>
export default {
props: ['text'],
emits: ['accepted']
}
</script>

来源:https://segmentfault.com/a/1190000039318888

标签:vue,组件,非兼容,变更
0
投稿

猜你喜欢

  • 用C语言操作MySQL数据库的通用方法

    2024-01-12 17:22:45
  • python print出共轭复数的方法详解

    2021-11-15 20:37:57
  • SQL Server性能调优之缓存

    2024-01-25 04:30:08
  • python sys.stdin和sys.stdout的用法说明

    2022-04-05 07:35:29
  • Python GUI之tkinter详解

    2021-09-11 06:36:01
  • 详解安装sql2012出现错误could not open key...解决办法

    2024-01-18 09:21:33
  • python实现逢七拍腿小游戏的思路详解

    2021-02-28 23:44:29
  • SQL SERVER 分组求和sql语句

    2024-01-13 04:43:16
  • 详解python 内存优化

    2022-06-04 16:31:43
  • 浅谈Python模块导入规范

    2021-02-03 03:39:37
  • Python各类图像库的图片读写方式总结(推荐)

    2021-05-20 00:50:22
  • Python Django Vue 项目创建过程详解

    2022-03-28 22:06:38
  • 详解python爬虫系列之初识爬虫

    2023-11-21 04:09:41
  • Python中url标签使用知识点总结

    2022-05-12 11:13:09
  • Vue中父组件向子组件通信的方法

    2024-04-26 17:37:32
  • Python实现全角半角字符互转的方法

    2022-07-29 10:03:25
  • python进行相关性分析并绘制散点图详解

    2023-02-25 14:45:54
  • sqlserver、mysql获取连接字符串步骤

    2024-01-22 00:49:33
  • 判断网页编码的方法python版

    2022-06-29 10:01:18
  • mysql 查询数据库中的存储过程与函数的语句

    2024-01-26 17:30:35
  • asp之家 网络编程 m.aspxhome.com