如何在Vue项目中应用TypeScript类

作者:灰灰 时间:2023-07-02 16:51:36 

目录
  • 一、前言

  • 二、使用

    • 1.@Component

    • 2.compued、data、methods

    • 3.@props

    • 4.@watch

    • 5.@emit

  • 三 、总结

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

    来源:https://www.tuicool.com/articles/m22ENbn

    标签:Vue,TypeScript
    0
    投稿

    猜你喜欢

  • Mysql 文件配置解析

    2024-01-26 10:01:54
  • python障碍式期权定价公式

    2023-12-08 03:54:53
  • Python数据分析库pandas高级接口dt的使用详解

    2023-12-02 22:33:19
  • Python使用Web框架Flask开发项目

    2021-07-08 20:08:56
  • 关于pip的安装,更新,卸载模块以及使用方法(详解)

    2022-03-15 07:21:36
  • 如何用CSS实现图像替换链接文本显示并保证链接可点击

    2011-03-03 12:37:00
  • Scrapy爬虫Response子类在应用中的问题解析

    2023-11-03 01:29:31
  • 对python实时得到鼠标位置的示例讲解

    2022-02-21 10:01:25
  • 解决python DataFrame 打印结果不换行问题

    2023-11-20 23:12:25
  • 一篇文章看懂SQL中的开窗函数

    2024-01-16 07:22:05
  • python 获取网页编码方式实现代码

    2023-07-21 08:15:14
  • mysql数据表和数据表关联

    2010-12-03 16:00:00
  • js+html5实现半透明遮罩层弹框效果

    2024-05-08 09:33:09
  • 轻松掌握如何从命令行启动mysqld服务器

    2008-12-31 15:47:00
  • 如何优化网站图片以快速显示

    2008-04-05 10:09:00
  • FastApi+Vue+LayUI实现前后端分离的示例代码

    2024-04-30 10:22:48
  • python中使用zip函数出现<zip object at 0x02A9E418>错误的原因

    2021-02-24 02:37:14
  • asp 数组 重复删除函数代码

    2011-03-03 10:47:00
  • CentOS 7下Python 2.7升级至Python3.6.1的实战教程

    2023-09-13 07:56:46
  • Pandas数据查询的集中实现方法

    2021-08-17 10:32:27
  • asp之家 网络编程 m.aspxhome.com