如何在Vue项目中应用TypeScript类

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了如何在Vue项目中应用TypeScript类脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

一、前言

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

主要的功能如下:

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

二、使用

vue-PRoperty-decorator 主要提供了以下装饰器

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

1.@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{ 
    
} 


2.COMpued、data、methods

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

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

3.@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; 
} 


4.@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) {} 
} 


5.@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与装饰器,但实际上本质是一致的,只有不断编写才会得心应手

到此这篇关于如何在Vue项目中应用TypeScript文章就介绍到这了,更多相关在Vue项目中应用TypeScript内容请搜索脚本宝典以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本宝典!

脚本宝典总结

以上是脚本宝典为你收集整理的如何在Vue项目中应用TypeScript类全部内容,希望文章能够帮你解决如何在Vue项目中应用TypeScript类所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: