1. 程式人生 > 程式設計 >如何在Vue專案中應用TypeScript類

如何在Vue專案中應用TypeScript類

目錄
  • 一、前言
  • 二、使用
    • 1.@Component
    • 2.compued、data、methods
    • 3.@props
    • 4.@watch
    • 5.@emit
  • 三 、總結

    如何在Vue專案中應用TypeScript類

    一、前言

    TypeScript是基於-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} frowww.cppcns.comm http://www.cppcns.com'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,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的語法與平時scrhttp://www.cppcns.comipt版本使用起來還是有很大的不同,多處用到class與裝飾器,但實際上本質是一致的,只有不斷編寫才會得心應手

    到此這篇關於如何在Vue專案中應用TypeScript的文章就介紹到這了,更多相關在Vue專案中應用TypeScript內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!