vue2升级vue3:class component的遗憾
在vue2,class 写法真的非常爽
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import { Component as tsc } from 'vue-tsx-support' ; import { Component, Watch } from 'vue-property-decorator' ; import { State } from 'vuex-class' ; import { debounce } from 'helpful-decorators' ; @Component export default class demo extends tsc<{}> { @State(state => state.queryContext.timeRange) curTimeRange; @Watch( '$route.params.uid' ) @debounce(500) initPage() { //TODO } } |
继承
1 2 3 4 5 | interface PageProps{} @Component export default class ChartWrapper extends Mixins<ChartWrapperBase>(ChartWrapperBase) implements PageProps { //TODO } |
但是到vue3,这个class 提案被废止了——GitHub上也停留在rc1版本了,已经2年左右没有提交代码了!
试了一下 8.0.0-rc.1
把 @Component 改为 @Options ,其他都不用怎么改。单个组件能跑。全局替换了下,发现大部分多页面与组件都跑步起来——报错的太多了——对后来人劝退!
vue-class-component made sense for Vue 2.x when the TypeScript support was really bad. Currently this library provides no additional benefits, it just modifies the syntax of declaring a component. Additionally almost all users of vue-class-component are using vue-property-decorator (https://github.com/kaorun343/vue-property-decorator) which also seems abandoned.
https://github.com/vuejs/vue-class-component/issues/569#issue-1021411164
class API 被犹大 彻底 放弃
Update: the Class API proposal is being dropped.
https://github.com/vuejs/rfcs/pull/17#issuecomment-494242121
Two major reasons:
Current Class API proposal still has various edge cases / spec reliance / unresolved questions.
The new APIs proposed in Advanced Reactivity API #22 and Dynamic Lifecycle Injection #23 enables a new pattern (temporarily) named "composition functions) that can serve as a better component API than classes. Shipping both composition functions and Class API essentially results in 3 ways of doing the same thing - this is something we want to avoid at all costs. The advantage of composition functions over Classes will be discussed in more details below.
无奈,不能平滑升级,给差评——有大佬会说,react 也准备弃用 class 了
但是,对于我这类从java 出身的野生前端,确实觉得class 装饰器 更为亲切
升级事项,查看:https://levelup.gitconnected.com/from-vue-class-component-to-composition-api-ef3c3dd5fdda
至于之的实现方式,建议换 函数式 思路,比如:https://www.thisdot.co/blog/vue-3-composition-api-do-you-really-need-it
虽然网上 有很多各种的 兼容原来 vue-class-component 装饰器方案,
https://github.com/darrenrahnemoon/vue-class-api
但是还是弃坑了 ——目前基于生态原因,只能用vue
其实心里一直是 react yyds!
转载本站文章《vue2升级vue3:class component的遗憾》,
请注明出处:https://www.zhoulujun.cn/html/webfront/ECMAScript/vue3/8844.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了