怎么在Vue项目中应用TypeScript类

发布时间:2021-09-15 12:44:27 作者:小新
来源:亿速云 阅读:131

这篇文章给大家分享的是有关怎么在Vue项目中应用TypeScript类的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

一、前言

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

主要的功能如下:

二、使用

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

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'); 
        } 
    }

感谢各位的阅读!关于“怎么在Vue项目中应用TypeScript类”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. 怎么在Vue2.4.0项目中使用$attrs与inheritAttrs
  2. typescript怎么在vue框架中使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue typescript

上一篇:python操作列表的方法有哪些

下一篇:JavaScript循环遍历的方法有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》