面試官:說(shuō)說(shuō)如何在Vue項(xiàng)目中應(yīng)用TypeScript?
一、前言
與如何在React項(xiàng)目中應(yīng)用TypeScript類(lèi)似
在VUE項(xiàng)目中應(yīng)用typescript,我們需要引入一個(gè)庫(kù)vue-property-decorator,
其是基于vue-class-component庫(kù)而來(lái),這個(gè)庫(kù)vue官方推出的一個(gè)支持使用class方式來(lái)開(kāi)發(fā)vue單文件組件的庫(kù)
主要的功能如下:
- methods 可以直接聲明為類(lèi)的成員方法
- 計(jì)算屬性可以被聲明為類(lèi)的屬性訪(fǎng)問(wèn)器
- 初始化的 data 可以被聲明為類(lèi)屬性
- data、render 以及所有的 Vue 生命周期鉤子可以直接作為類(lèi)的成員方法
- 所有其他屬性,需要放在裝飾器中
二、使用
vue-property-decorator 主要提供了以下裝飾器
- @Prop
- @PropSync
- @Model
- @Watch
- @Provide
- @Inject
- @ProvideReactive
- @InjectReactive
- @Emit
- @Ref
- @Component (由 vue-class-component 提供)
- Mixins (由 vue-class-component 提供)
@Component
Component裝飾器它注明了此類(lèi)為一個(gè)Vue組件,因此即使沒(méi)有設(shè)置選項(xiàng)也不能省略
如果需要定義比如 name、components、filters、directives以及自定義屬性,就可以在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返回對(duì)象中的屬性、methods中的方法需要直接定義在Class中,當(dāng)做類(lèi)的屬性和方法
- @Component
- export default class HelloDecorator extends Vue {
- count: number = 123 // 類(lèi)屬性相當(dāng)于以前的 data
- add(): number { // 類(lèi)方法就是以前的方法
- this.count + 1
- }
- // 獲取計(jì)算屬性
- get total(): number {
- return this.count + 1
- }
- // 設(shè)置計(jì)算屬性
- 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
- //如果是對(duì)象或數(shù)組的話(huà)。默認(rèn)值從一個(gè)工廠函數(shù)中返回
- // defatult: () => {
- // return ['a','b']
- // }
- required: true,
- validator: (value) => {
- return [
- 'InProcess',
- 'Settled'
- ].indexOf(value) !== -1
- }
- })
- propC:string;
- }
@watch
實(shí)際就是Vue中的監(jiān)聽(tīng)器,如下:
- 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中的事件的觸發(fā)$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');
- }
- }
三 、總結(jié)
可以看到上述typescript版本的vue class的語(yǔ)法與平時(shí)javascript版本使用起來(lái)還是有很大的不同,多處用到class與裝飾器,但實(shí)際上本質(zhì)是一致的,只有不斷編寫(xiě)才會(huì)得心應(yīng)手