Vue 源码中 Vue 为什么不用class来构造, 而使用了函数呢 ?

在这里插入图片描述
在这里插入图片描述

Vue之所以没有选择用es6的 Class 是因为 Class 本身扩展性不强. Vue将所有的功能都分布到多个模块中, 函数才具有这样的扩展性并且 Vue 源码中某些地方改过 this 指向, class 也是不具备的.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue3使用TypeScript可以带来许多好处。首先,TypeScript是JavaScript的一个超集,它提供了类型系统和对ES6的支持,这可以增加代码的可读性和可维护性。在Vue3,官方已经使用TypeScript编写了源码,这也是Vue3升级的一个亮点。\[1\] 在Vue3,你可以直接使用TypeScript来定义组件。你可以使用`defineComponent`函数来定义组件,并使用`PropType`来定义组件的属性类型。例如,你可以这样写一个组件: ```typescript import { defineComponent, PropType } from 'vue'; interface Student { name: string; class: string; age: number; } const Component = defineComponent({ props: { success: { type: String }, callback: { type: Function as PropType<() => void> }, student: { type: Object as PropType<Student>, required: true } }, data() { return { message: 'Vue3 code style' }; }, computed: { reversedMessage(): string { return this.message.split('').reverse().join(''); } } }); ``` 这样,你就可以在Vue3使用TypeScript来定义组件的属性类型,并且享受到类型检查的好处。\[2\] 当然,在初期使用TypeScript时,一些开发者可能会倾向于使用`any`类型,这样会失去TypeScript的类型检查的意义。但是,养成写类型的习惯是很重要的,可以逐渐提高代码的可靠性和可维护性。所以,在使用Vue3和TypeScript时,建议尽量避免使用`any`类型,而是养成写类型的好习惯。\[3\] #### 引用[.reference_title] - *1* *2* *3* [Vue3.0+TS 项目上手教程](https://blog.csdn.net/Ed7zgeE9X/article/details/109039793)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值