日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
面試官:說說如何在Vue項目中應(yīng)用TypeScript?

一、前言

與如何在React項目中應(yīng)用TypeScript類似

在VUE項目中應(yīng)用typescript,我們需要引入一個庫vue-property-decorator,

其是基于vue-class-component庫而來,這個庫vue官方推出的一個支持使用class方式來開發(fā)vue單文件組件的庫

主要的功能如下:

  • methods 可以直接聲明為類的成員方法
  • 計算屬性可以被聲明為類的屬性訪問器
  • 初始化的 data 可以被聲明為類屬性
  • data、render 以及所有的 Vue 生命周期鉤子可以直接作為類的成員方法
  • 所有其他屬性,需要放在裝飾器中

二、使用

vue-property-decorator 主要提供了以下裝飾器

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

@Component

Component裝飾器它注明了此類為一個Vue組件,因此即使沒有設(shè)置選項也不能省略

如果需要定義比如 name、components、filters、directives以及自定義屬性,就可以在Component裝飾器中定義,如下:

 
 
 
 
  1. import {Component,Vue} from 'vue-property-decorator';
  2. import {componentA,componentB} from '@/components';
  3.  @Component({
  4.     components:{
  5.         componentA,
  6.         componentB,
  7.     },
  8.     directives: {
  9.         focus: {
  10.             // 指令的定義
  11.             inserted: function (el) {
  12.                 el.focus()
  13.             }
  14.         }
  15.     }
  16. })
  17. export default class YourCompoent extends Vue{
  18.    
  19. }

computed、data、methods

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

 
 
 
 
  1. @Component
  2. export default class HelloDecorator extends Vue {
  3.     count: number = 123 // 類屬性相當(dāng)于以前的 data
  4.     add(): number { // 類方法就是以前的方法
  5.         this.count + 1
  6.     }
  7.     // 獲取計算屬性
  8.     get total(): number {
  9.       return this.count + 1
  10.     }
  11.     // 設(shè)置計算屬性
  12.     set total(param:number): void {
  13.       this.count = param
  14.     }
  15. }

@props

組件接收屬性的裝飾器,如下使用:

 
 
 
 
  1. import {Component,Vue,Prop} from vue-property-decorator;
  2. @Component
  3. export default class YourComponent extends Vue {
  4.     @Prop(String)
  5.     propA:string;
  6.     
  7.     @Prop([String,Number])
  8.     propB:string|number;
  9.     
  10.     @Prop({
  11.      type: String, // type: [String , Number]
  12.      default: 'default value', // 一般為String或Number
  13.       //如果是對象或數(shù)組的話。默認(rèn)值從一個工廠函數(shù)中返回
  14.       // defatult: () => {
  15.       //     return ['a','b']
  16.       // }
  17.      required: true,
  18.      validator: (value) => {
  19.         return [
  20.           'InProcess',
  21.           'Settled'
  22.         ].indexOf(value) !== -1
  23.      }
  24.     })
  25.     propC:string;
  26. }

@watch

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

 
 
 
 
  1. import { Vue, Component, Watch } from 'vue-property-decorator'
  2. @Component
  3. export default class YourComponent extends Vue {
  4.   @Watch('child')
  5.   onChildChanged(val: string, oldVal: string) {}
  6.   @Watch('person', { immediate: true, deep: true })
  7.   onPersonChanged1(val: Person, oldVal: Person) {}
  8.   @Watch('person')
  9.   onPersonChanged2(val: Person, oldVal: Person) {}
  10. }

@emit

vue-property-decorator 提供的 @Emit 裝飾器就是代替Vue中的事件的觸發(fā)$emit,如下:

 
 
 
 
  1. import {Vue, Component, Emit} from 'vue-property-decorator';
  2.     @Component({})
  3.     export default class Some extends Vue{
  4.         mounted(){
  5.             this.$on('emit-todo', function(n) {
  6.                 console.log(n)
  7.             })
  8.             this.emitTodo('world');
  9.         }
  10.         @Emit()
  11.         emitTodo(n: string){
  12.             console.log('hello');
  13.         }
  14.     }

三 、總結(jié)

可以看到上述typescript版本的vue class的語法與平時javascript版本使用起來還是有很大的不同,多處用到class與裝飾器,但實際上本質(zhì)是一致的,只有不斷編寫才會得心應(yīng)手


分享題目:面試官:說說如何在Vue項目中應(yīng)用TypeScript?
標(biāo)題來源:http://www.dlmjj.cn/article/djoegps.html