面试官:说说如何在Vue项目中应用TypeScript?

开发 前端
在VUE项目中应用typescript,我们需要引入一个库vue-property-decorator,基于vue-class-component库而来,这个库vue官方推出的一个支持使用class方式来开发vue单文件组件的库。

[[423983]]

一、前言

与如何在React项目中应用TypeScript类似

在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组件,因此即使没有设置选项也不能省略

如果需要定义比如 name、components、filters、directives以及自定义属性,就可以在Component装饰器中定义,如下:

  1. import {Component,Vue} from 'vue-property-decorator'
  2. import {componentA,componentB} from '@/components'
  3.  
  4.  @Component({ 
  5.     components:{ 
  6.         componentA, 
  7.         componentB, 
  8.     }, 
  9.     directives: { 
  10.         focus: { 
  11.             // 指令的定义 
  12.             inserted: function (el) { 
  13.                 el.focus() 
  14.             } 
  15.         } 
  16.     } 
  17. }) 
  18. export default class YourCompoent extends Vue{ 
  19.     

computed、data、methods

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

  1. @Component 
  2. export default class HelloDecorator extends Vue { 
  3.     count: number = 123 // 类属性相当于以前的 data 
  4.  
  5.     add(): number { // 类方法就是以前的方法 
  6.         this.count + 1 
  7.     } 
  8.  
  9.     // 获取计算属性 
  10.     get total(): number { 
  11.       return this.count + 1 
  12.     } 
  13.  
  14.     // 设置计算属性 
  15.     set total(param:number): void { 
  16.       this.count = param 
  17.     } 

@props

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

  1. import {Component,Vue,Prop} from vue-property-decorator; 
  2.  
  3. @Component 
  4. export default class YourComponent extends Vue { 
  5.     @Prop(String) 
  6.     propA:string; 
  7.      
  8.     @Prop([String,Number]) 
  9.     propB:string|number; 
  10.      
  11.     @Prop({ 
  12.      type: String, // type: [String , Number] 
  13.      default'default value', // 一般为String或Number 
  14.       //如果是对象或数组的话。默认值从一个工厂函数中返回 
  15.       // defatult: () => { 
  16.       //     return ['a','b'
  17.       // } 
  18.      required: true
  19.      validator: (value) => { 
  20.         return [ 
  21.           'InProcess'
  22.           'Settled' 
  23.         ].indexOf(value) !== -1 
  24.      } 
  25.     }) 
  26.     propC:string; 

@watch

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

  1. import { Vue, Component, Watch } from 'vue-property-decorator' 
  2.  
  3. @Component 
  4. export default class YourComponent extends Vue { 
  5.   @Watch('child'
  6.   onChildChanged(val: string, oldVal: string) {} 
  7.  
  8.   @Watch('person', { immediate: true, deep: true }) 
  9.   onPersonChanged1(val: Person, oldVal: Person) {} 
  10.  
  11.   @Watch('person'
  12.   onPersonChanged2(val: Person, oldVal: Person) {} 

@emit

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

  1. import {Vue, Component, Emit} from 'vue-property-decorator'
  2.     @Component({}) 
  3.     export default class Some extends Vue{ 
  4.         mounted(){ 
  5.             this.$on('emit-todo'function(n) { 
  6.                 console.log(n) 
  7.             }) 
  8.             this.emitTodo('world'); 
  9.         } 
  10.         @Emit() 
  11.         emitTodo(n: string){ 
  12.             console.log('hello'); 
  13.         } 
  14.     } 

三 、总结

 

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

 

责任编辑:武晓燕 来源: JS每日一题
相关推荐

2021-09-14 07:06:13

React项目TypeScript

2021-09-06 10:51:27

TypeScriptJavaScript

2021-09-08 07:49:34

TypeScript 泛型场景

2021-09-10 06:50:03

TypeScript装饰器应用

2021-05-20 08:34:03

CDN原理网络

2024-03-05 10:33:39

AOPSpring编程

2021-08-03 07:51:43

React项目面试

2021-05-31 10:35:34

TCPWebSocket协议

2021-09-29 07:24:20

场景数据

2021-09-28 07:12:09

测试路径

2021-09-13 09:23:52

TypeScript命名空间

2023-12-27 18:16:39

MVCC隔离级别幻读

2024-02-29 16:49:20

volatileJava并发编程

2023-12-19 09:24:22

LinuxBIOSUEFI

2021-09-16 07:52:18

算法应用场景

2021-07-07 08:36:45

React应用场景

2021-07-12 08:35:24

组件应用场景

2021-10-09 10:25:41

排序应用场景

2021-10-08 09:59:32

冒泡排序场景

2021-10-13 18:01:33

快速排序场景
点赞
收藏

51CTO技术栈公众号