溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Vue項目中應(yīng)用TypeScript類

發(fā)布時間:2021-09-15 12:44:27 來源:億速云 閱讀:137 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)怎么在Vue項目中應(yīng)用TypeScript類的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

    一、前言

    TypeScript是基于vue-class-component庫而來,這個庫vue官方推出的一個支持使用class方式來開發(fā)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組件,因此即使沒有設(shè)置選項也不能省略

    如果需要定義比如 namecomponents、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{ 
        
    }

    2.compued、data、methods

    這里取消了組件的datamethods屬性,以往data返回對象中的屬性、methods中的方法需要直接定義在Class中,當(dāng)做類的屬性和方法

    @Component 
    export default class HelloDecorator extends Vue { 
        count: number = 123 // 類屬性相當(dāng)于以前的 data 
     
        add(): number { // 類方法就是以前的方法 
            this.count + 1 
        } 
     
        // 獲取計算屬性 
        get total(): number { 
          return this.count + 1 
        } 
     
        // 設(shè)置計算屬性 
        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 
          //如果是對象或數(shù)組的話。默認(rèn)值從一個工廠函數(shù)中返回 
          // defatult: () => { 
          //     return ['a','b'] 
          // } 
         required: true, 
         validator: (value) => { 
            return [ 
              'InProcess', 
              'Settled' 
            ].indexOf(value) !== -1 
         } 
        }) 
        propC:string; 
    }

    4.@watch

    實際就是Vue中的監(jiān)聽器,如下:

    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中的事件的觸發(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'); 
            } 
        }

    感謝各位的閱讀!關(guān)于“怎么在Vue項目中應(yīng)用TypeScript類”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI