当前位置: 首页 > news >正文

vue2 vue-property-decorator 库就类似于Java的注解库 vue class类编程

安装

npm install vue-property-decorator vue-class-component --save
  • 依赖关系:

  • vue-class-component 提供了 Vue 的类式写法支持。

  • vue-property-decorator 基于 vue-class-component,封装了一些常用的装饰器。

基本用法

<template><div><h1>{{ msg }}</h1><button @click="increment">Count: {{ count }}</button></div>
</template><script lang="ts">
import { Vue, Component } from 'vue-property-decorator'@Component
export default class HelloWorld extends Vue {// 普通 datamsg: string = 'Hello Vue2 + TS'count: number = 0// 方法increment() {this.count++}
}
</script>

. 常用装饰器

  • @Prop —— 定义 props
import { Vue, Component, Prop } from 'vue-property-decorator'@Component
export default class Child extends Vue {@Prop({ type: String, required: true }) readonly title!: string@Prop({ type: Number, default: 0 }) readonly count!: number
}@Prop(String) readonly name!: string;
@Prop({ type: Number, default: 18 }) readonly age!: number;
  • @Emit —— 定义自定义事件
import { Vue, Component, Emit } from 'vue-property-decorator'@Component
export default class Child extends Vue {count = 0@Emit('update') // 事件名为 "update"increment(step: number) {this.count += stepreturn this.count // 会作为事件参数 emit 出去}
}
<Child @update="val => console.log(val)" />
  • @Watch —— 监听数据变化
import { Vue, Component, Watch } from 'vue-property-decorator'@Component
export default class MyComp extends Vue {count = 0@Watch('count', { immediate: true, deep: false })onCountChange(newVal: number, oldVal: number) {console.log(`count: ${oldVal} -> ${newVal}`)}
}
  • @Provide / @Inject —— 依赖注入
import { Vue, Component, Provide } from 'vue-property-decorator'@Component
export default class Parent extends Vue {@Provide('theme') theme = 'dark'
}

子组件:

import { Vue, Component, Inject } from 'vue-property-decorator'@Component
export default class Child extends Vue {@Inject('theme') readonly theme!: string
}
  • 生命周期钩子
import { Vue, Component } from 'vue-property-decorator'@Component
export default class Example extends Vue {created() {console.log('组件已创建')}mounted() {console.log('组件已挂载')}
}
  • 计算属性 (get/set)
import { Vue, Component } from 'vue-property-decorator'@Component
export default class Example extends Vue {firstName = 'Tom'lastName = 'Jerry'get fullName() {return `${this.firstName} ${this.lastName}`}set fullName(val: string) {const [first, last] = val.split(' ')this.firstName = firstthis.lastName = last}
}

完整示例

<template><div><h1>{{ title }}</h1><p>Count: {{ count }}</p><button @click="increment(2)">+2</button><Child :title="title" :count="count" @update="onUpdate" /></div>
</template><script lang="ts">
import { Vue, Component, Prop, Emit, Watch } from 'vue-property-decorator'
import Child from './Child.vue'@Component({ components: { Child } })
export default class Parent extends Vue {title: string = '父组件标题'count: number = 0@Watch('count')onCountChange(newVal: number, oldVal: number) {console.log(`父组件 count: ${oldVal} -> ${newVal}`)}@Emit('update-count')increment(step: number) {this.count += stepreturn this.count}onUpdate(val: number) {this.count = val}
}
</script>

vue-property-decorator 仅适用于 Vue 2,Vue 3 已弃用。

写法更接近 OOP,但不符合 Vue 3 官方推荐的组合式 API。

装饰器语法在 TS 中更优雅,但学习成本比传统 export default {} 高。

团队如果有 Angular/Java 背景会觉得更自然,否则 Vue 初学者更容易迷惑。


文章转载自:

http://xxXLS4Fj.qmwzr.cn
http://Umw8GELj.qmwzr.cn
http://lu0Bccz3.qmwzr.cn
http://NtXAcwwt.qmwzr.cn
http://5XYVAtr1.qmwzr.cn
http://XFuThaJy.qmwzr.cn
http://GWbcjQEe.qmwzr.cn
http://DK7oXMOe.qmwzr.cn
http://thXkXRcG.qmwzr.cn
http://TmkGhew3.qmwzr.cn
http://460h5NYw.qmwzr.cn
http://zqjqcetO.qmwzr.cn
http://GSHQuMvG.qmwzr.cn
http://olKNmdgZ.qmwzr.cn
http://nzAmFB3n.qmwzr.cn
http://ehwaS9Mm.qmwzr.cn
http://DJDApXyn.qmwzr.cn
http://Ns7MKU7o.qmwzr.cn
http://GLl59XYH.qmwzr.cn
http://W55qVJGt.qmwzr.cn
http://SqDzRYK0.qmwzr.cn
http://w6CByhP7.qmwzr.cn
http://cudA9wm8.qmwzr.cn
http://cxfkXpJu.qmwzr.cn
http://PKj1s7kA.qmwzr.cn
http://usHLm95w.qmwzr.cn
http://OKPefQSf.qmwzr.cn
http://xso6dpjQ.qmwzr.cn
http://uGf9exfe.qmwzr.cn
http://I2FTjWqi.qmwzr.cn
http://www.dtcms.com/a/363264.html

相关文章:

  • 阿里云和华为云Rocky LINUX 9.X镜像就绪及低端可用英伟达GPU
  • 力扣hot100:除自身以外数组的乘积(除法思路和左右前缀乘积)(238)
  • 静态ip软件哪个好用?资深用户的选择指南
  • Vite 插件 @vitejs/plugin-legacy 深度解析:旧浏览器兼容指南
  • 快速实现PLC之间的通信-基恩士
  • Spring Boot 全局字段处理最佳实践
  • 【程序员必备的Linux信号处理知识】
  • 【通用视觉框架】基于Python+OpenCV+PyQt5开发的视觉框架软件,全套源码,开箱即用
  • 变频器实习DAY41 单元测试介绍
  • % g++ *.cpp ...: fatal error: ‘opencv2/opencv.hpp‘ file not found 1
  • 趣味学RUST基础篇(错误处理)
  • Delphi 5 操作Word表格选区问题解析
  • 大数据毕业设计选题推荐-基于大数据的电脑硬件数据分析系统-Hadoop-Spark-数据可视化-BigData
  • 水电站电动机绝缘安全 “不掉线”!在线监测方案筑牢发电保障
  • ReactAgent接入MCP服务工具
  • 拷打字节面试官之-吃透c语言-哈希算法 如何在3面拷打字节cto 3万行算法源码带你吃透算法面试所有考题
  • C/C++条件编译:深入理解#ifndef/#endif守卫
  • 20.Linux进程信号(一)
  • C++拷贝语义和移动语义,左值引用与右值引用
  • 汉得H-AI飞码智能编码助手V1.2.4正式发布!
  • Turso数据库:用Rust重构的下一代SQLite——轻量级嵌入式数据库的未来选择
  • 三维重建——基础理论(四):三维重建基础与极几何原理(三维重建基础、单视图回忆、双目视觉、极几何、本质矩阵与基础矩阵、基础矩阵估计)
  • 虚实交互新突破:Three.js融合AR技术的孪生数据操控方法
  • 什么是 AWS 和 GCE ?
  • 解决Mac电脑连接蓝牙鼠标的延迟问题
  • 对于牛客网—语言学习篇—编程初学者入门训练—复合类型:BC140 杨辉三角、BC133 回型矩阵、BC134 蛇形矩阵题目的解析
  • A-Level课程选择与机构报名指南
  • 净利润超10亿元,智能类产品18倍增长!顾家家居2025年半年报业绩:零售增长强劲,整家定制多维突破,全球深化布局!|商派
  • Selenium核心技巧:元素定位与等待策略
  • 苹果内部 AI聊天机器人“Asa”曝光,为零售员工打造专属A