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

01.前端面试题之ts:说说如何在Vue项目中应用TypeScript?

文章目录

    • 一、前言
    • 二、使用
      • @Component
      • computed、data、methods
      • @props
      • @watch
      • @emit
    • 三 、总结

一、前言

与link类似

VUE项目中应用typescript,我们需要引入一个库vue-property-decorator

其是基于vue-class-component库而来,这个库vue官方推出的一个支持使用class方式来开发vue单文件组件的库

主要的功能如下:

  • methods 可以直接声明为类的成员方法
  • 计算属性可以被声明为类的属性访问器
  • 初始化的 data 可以被声明为类属性
  • data、render 以及所有的 Vue 生命周期钩子可以直接作为类的成员方法
  • 所有其他属性,需要放在装饰器中

二、使用

vue-property-decorator 主要提供了多个装饰器和一个函数:

  • @Prop
  • @PropSync
  • @Model
  • @Watch
  • @Provide
  • @Inject
  • @ProvideReactive
  • @InjectReactive
  • @Emit
  • @Ref
  • @Component (由 vue-class-component 提供)
  • Mixins (由 vue-class-component 提供)

@Component

Component装饰器它注明了此类为一个Vue组件,因此即使没有设置选项也不能省略

如果需要定义比如 namecomponentsfiltersdirectives以及自定义属性,就可以在Component装饰器中定义,如下:

import {Component,Vue} from 'vue-property-decorator';
import {componentA,componentB} from '@/components';@Component({components:{componentA,componentB,},directives: {focus: {// 指令的定义inserted: function (el) {el.focus()}}}
})
export default class YourCompoent extends Vue{}

computed、data、methods

这里取消了组件的data和methods属性,以往data返回对象中的属性、methods中的方法需要直接定义在Class中,当做类的属性和方法

@Component
export default class HelloDecorator extends Vue {count: number = 123 // 类属性相当于以前的 dataadd(): number { // 类方法就是以前的方法this.count + 1}// 获取计算属性get total(): number {return this.count + 1}// 设置计算属性set total(param:number): void {this.count = param}
}

@props

组件接收属性的装饰器,如下使用:

import {Component,Vue,Prop} from vue-property-decorator;@Component
export default class YourComponent extends Vue {@Prop(String)propA:string;@Prop([String,Number])propB:string|number;@Prop({type: String, // type: [String , Number]default: 'default value', // 一般为String或Number//如果是对象或数组的话。默认值从一个工厂函数中返回// defatult: () => {//     return ['a','b']// }required: true,validator: (value) => {return ['InProcess','Settled'].indexOf(value) !== -1}})propC:string;
}

@watch

实际就是Vue中的监听器,如下:

import { Vue, Component, Watch } from 'vue-property-decorator'@Component
export default class YourComponent extends Vue {@Watch('child')onChildChanged(val: string, oldVal: string) {}@Watch('person', { immediate: true, deep: true })onPersonChanged1(val: Person, oldVal: Person) {}@Watch('person')onPersonChanged2(val: Person, oldVal: Person) {}
}

@emit

vue-property-decorator 提供的 @Emit 装饰器就是代替Vue 中的事件的触发$emit,如下:

import {Vue, Component, Emit} from 'vue-property-decorator';@Component({})export default class Some extends Vue{mounted(){this.$on('emit-todo', function(n) {console.log(n)})this.emitTodo('world');}@Emit()emitTodo(n: string){console.log('hello');}}

三 、总结

可以看到上述typescript版本的vue class的语法与平时javascript版本使用起来还是有很大的不同,多处用到class与装饰器,但实际上本质是一致的,只有不断编写才会得心应手

http://www.lryc.cn/news/446989.html

相关文章:

  • 【HTTP】方法(method)以及 GET 和 POST 的区别
  • Ubuntu NFS 搭建及配置
  • 双十一好物推荐,这些值得入手的宝藏产品
  • 秋招内推2025--招联金融
  • C++类和对象——第二关
  • 服务器数据恢复—raid5阵列热备盘上线失败导致阵列崩溃的数据恢复案例
  • Python与SQL Server数据库结合导出Excel并做部分修改
  • 常见的TTL,RS232,RS485,IIC,SPI,UART之间的联系和区别
  • 【数据结构】栈和队列(Stack Queue)
  • Vue.js基础
  • 罐区紧急切断阀安装位置规范
  • JavaScript 中的事件模型
  • 理解Java引用数据类型(数组、String)传参机制的一个例子
  • 【计算机组成原理】实验一:运算器输入锁存器数据写实验
  • LSI SAS 9361-8i和SAS3008 12 gb / s PCIe 3.0 RAID 阵列卡配置
  • node js版本低导致冲突WARN EBADENGINE package: required: { node: ‘>=18‘ }
  • 828华为云征文|使用Flexus X实例安装宝塔面板教学
  • 1.量化第一步,搭建属于自己的金融数据库!
  • git-repo系列教程(6) 在自己服务器上搭建git-repo仓库
  • 微服务——服务保护(Sentinel)(一)
  • jenkins声明式流水线语法详解
  • mini-lsm通关笔记Week2Overview
  • 基于SpringBoot的在线点餐系统【附源码】
  • 生成式语言模型底层技术面试
  • HTML开发指南
  • 共筑数据安全防线!YashanDB与SPU完成兼容性互认证
  • 【FastAPI】使用FastAPI和Redis实现实时通知(SSE)
  • Keyence_PL_MC_HslCommunication import MelsecMcNet
  • 软件架构的演变与趋势(软件架构演变的阶段、综合案例分析:在线电商平台架构演变、开发补充)
  • Shopify独立站运营必知必会:选品与防封技巧