如何在Vue項目中應(yīng)用TypeScript類

一、前言
TypeScript是基于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 提供)
1.@Component
Component裝飾器它注明了此類為一個Vue組件,因此即使沒有設(shè)置選項也不能省略
如果需要定義比如 name、components、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
這里取消了組件的data和methods屬性,以往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');
}
}
三 、總結(jié)
可以看到上述typescript版本的vue class的語法與平時javascript版本使用起來還是有很大的不同,多處用到class與裝飾器,但實際上本質(zhì)是一致的,只有不斷編寫才會得心應(yīng)手
到此這篇關(guān)于如何在Vue項目中應(yīng)用TypeScript的文章就介紹到這了,更多相關(guān)在Vue項目中應(yīng)用TypeScript內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue路由跳轉(zhuǎn)傳參或打開新頁面跳轉(zhuǎn)的方法總結(jié)
這篇文章主要給大家介紹了關(guān)于Vue路由跳轉(zhuǎn)傳參或打開新頁面跳轉(zhuǎn)的相關(guān)資料,在使用Vue.js開發(fā)單頁面應(yīng)用時常常會遇到路由跳轉(zhuǎn)傳參的需求,需要的朋友可以參考下2023-07-07
解決iview多表頭動態(tài)更改列元素發(fā)生的錯誤的方法
這篇文章主要介紹了解決iview多表頭動態(tài)更改列元素發(fā)生的錯誤的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
vue兩組件間值傳遞 $router.push實現(xiàn)方法
兩組件間傳值,可能包含多種情況,這篇文章主要介紹了vue兩組件間值傳遞 $router.push實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05

