如何在Vue項(xiàng)目中應(yīng)用TypeScript類
一、前言
TypeScript
是基于vue-class-component
庫(kù)而來,這個(gè)庫(kù)vue官方推出的一個(gè)支持使用class
方式來開發(fā)vue
單文件組件的庫(kù)
主要的功能如下:
methods
可以直接聲明為類的成員方法- 計(jì)算屬性可以被聲明為類的屬性訪問器
- 初始化的
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
裝飾器它注明了此類為一個(gè)Vue組件,因此即使沒有設(shè)置選項(xiàng)也不能省略
如果需要定義比如 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返回對(duì)象中的屬性、methods
中的方法需要直接定義在Class中,當(dāng)做類的屬性和方法
@Component export default class HelloDecorator extends Vue { count: number = 123 // 類屬性相當(dāng)于以前的 data add(): number { // 類方法就是以前的方法 this.count + 1 } // 獲取計(jì)算屬性 get total(): number { return this.count + 1 } // 設(shè)置計(jì)算屬性 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 //如果是對(duì)象或數(shù)組的話。默認(rèn)值從一個(gè)工廠函數(shù)中返回 // defatult: () => { // return ['a','b'] // } required: true, validator: (value) => { return [ 'InProcess', 'Settled' ].indexOf(value) !== -1 } }) propC:string; }
4.@watch
實(shí)際就是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
的語(yǔ)法與平時(shí)javascript
版本使用起來還是有很大的不同,多處用到class與裝飾器,但實(shí)際上本質(zhì)是一致的,只有不斷編寫才會(huì)得心應(yīng)手
到此這篇關(guān)于如何在Vue項(xiàng)目中應(yīng)用TypeScript
的文章就介紹到這了,更多相關(guān)在Vue
項(xiàng)目中應(yīng)用TypeScript
內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue路由跳轉(zhuǎn)傳參或打開新頁(yè)面跳轉(zhuǎn)的方法總結(jié)
這篇文章主要給大家介紹了關(guān)于Vue路由跳轉(zhuǎn)傳參或打開新頁(yè)面跳轉(zhuǎn)的相關(guān)資料,在使用Vue.js開發(fā)單頁(yè)面應(yīng)用時(shí)常常會(huì)遇到路由跳轉(zhuǎn)傳參的需求,需要的朋友可以參考下2023-07-07解決iview多表頭動(dòng)態(tài)更改列元素發(fā)生的錯(cuò)誤的方法
這篇文章主要介紹了解決iview多表頭動(dòng)態(tài)更改列元素發(fā)生的錯(cuò)誤的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11vue兩組件間值傳遞 $router.push實(shí)現(xiàn)方法
兩組件間傳值,可能包含多種情況,這篇文章主要介紹了vue兩組件間值傳遞 $router.push實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05