在Vue.js中使用TypeScript的方法
雖然 vue2.x 對(duì)TypeScript的支持還不是非常完善,但是從今年即將到來(lái)的3.0版本在GitHub上的倉(cāng)庫(kù) vue-next 看,為T(mén)S提供更好的官方支持應(yīng)該也會(huì)是一個(gè)重要特性,那么,在迎接3.0之前,不妨先來(lái)看看目前版本二者的搭配食用方法吧~
創(chuàng)建項(xiàng)目
- 雖然GitHub上有各種各樣相關(guān)的Starter,但是使用
Vue CLI應(yīng)該是目前相對(duì)比較好的方式,在使用vue create創(chuàng)建新項(xiàng)目時(shí),對(duì)preset選擇Manually select features選項(xiàng),之后添加TypeScript - 如果想在vue應(yīng)用中完整使用ES6中提供的類特性,那么在
class-style component syntax處選擇Y(本文主要介紹選擇Y的情況) - 對(duì)于
Babel來(lái)說(shuō),一般情況選擇使用,而linter / formatter的具體選擇可根據(jù)項(xiàng)目需求,此處不多說(shuō)明
進(jìn)入項(xiàng)目
創(chuàng)建完成后,看一看 package.json ,可以發(fā)現(xiàn) vue-class-component 和 vue-property-decorator 以及其他ts相關(guān)的modules都已被添加,其中: vue-class-component 可以讓你使用class-style語(yǔ)法創(chuàng)建組件,比如以下代碼:
<template>
<div>
<button @click="decrement">-</button>
{{ count }}
<button @click="increment">+</button>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'
// Define the component in class-style
@Component
export default class Counter extends Vue {
// Class properties will be component data
count = 0
// Methods will be component methods
increment() {
this.count++
}
decrement() {
this.count--
}
}
</script>
而 vue-property-component 則完全依賴于前者,提供了除 @Component 外的其他幾種裝飾器,比如 @Prop
import { Vue, Component, Prop } from 'vue-property-decorator'
@Component
export default class YourComponent extends Vue {
@Prop(Number) readonly propA: number | undefined
@Prop({ default: 'default value' }) readonly propB!: string
@Prop([String, Boolean]) readonly propC: string | boolean | undefined
}
再來(lái)一個(gè)二者結(jié)合的簡(jiǎn)單例子吧:
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<h1>{{ fullName }}</h1>
<button @click="reverseStr()">Reverse</button>
</div>
</template>
<script lang="ts">
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
@Component
export default class HelloWorld extends Vue {
@Prop() private msg!: string;
firstName = "rapt";
lastName = "azure";
mounted() {
console.log('mounted');
}
// Computed property
get fullName(): string {
return this.firstName + this.lastName;
}
// Method
reverseStr() {
this.firstName = this.firstName.split('').reverse().join('');
this.lastName = this.lastName.split('').reverse().join('');
}
}
</script>
- 此時(shí),你的vue項(xiàng)目已經(jīng)有fully-typed的可能了,當(dāng)然也會(huì)有更好的自動(dòng)補(bǔ)全以及錯(cuò)誤提示。
- 為了更好的確定類型,可以創(chuàng)建例如
interfaces這樣的文件夾,充分利用ts的接口和類來(lái)使項(xiàng)目有更好的組織結(jié)構(gòu),可讀性和維護(hù)性。
另一種選擇
其實(shí)當(dāng)然也可以不使用class風(fēng)格啦,這樣的話,就和平時(shí)熟悉的vue更為相似了,而對(duì)類型當(dāng)然也是完全支持的。
這里也提供一個(gè)簡(jiǎn)單的例子吧~<template>
<div class="hello">
<h1>{{ msg }}</h1>
<h1>{{ test }}</h1>
</div>
</template>
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
name: 'HelloWorld',
props: {
msg: String,
},
data() {
return {
test: "Hello from TS" as string
}
},
methods: {
pressMe(): string {
return this.test + 'br'
}
}
});
</script>
其他的話
- 本文只是簡(jiǎn)要探討了在Vue.js中使用TypeScript的可能性,更多的相關(guān)內(nèi)容在 官方文檔 里可以找到哦,或者也可以多去Github的Vue區(qū),TS區(qū)逛逛呢~
- TypeScript的出現(xiàn)為JavaScript的生態(tài)帶來(lái)了新活力,不管是前端三大框架Vue,React,Angular,還是Node系的后端框架比如Nest和Express,都在積極擁抱TS,希望以后整個(gè)生態(tài)會(huì)發(fā)展得越來(lái)越好吧~
總結(jié)
到此這篇關(guān)于在Vue.js中使用TypeScript的文章就介紹到這了,更多相關(guān)Vue.js使用TypeScript內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解JavaScript私有類字段和TypeScript私有修飾符
- JS裝飾者模式和TypeScript裝飾器
- JavaScript/TypeScript 實(shí)現(xiàn)并發(fā)請(qǐng)求控制的示例代碼
- RxJS在TypeScript中的簡(jiǎn)單使用詳解
- JavaScript和TypeScript中的void的具體使用
- typescript nodejs 依賴注入實(shí)現(xiàn)方法代碼詳解
- vue + typescript + video.js實(shí)現(xiàn) 流媒體播放 視頻監(jiān)控功能
- 手把手教你使用TypeScript開(kāi)發(fā)Node.js應(yīng)用
- JavaScript?與?TypeScript之間的聯(lián)系
相關(guān)文章
vue項(xiàng)目中的遇錯(cuò):Invalid?Host?header問(wèn)題
這篇文章主要介紹了vue項(xiàng)目中的遇錯(cuò):Invalid?Host?header問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vue.js+elementUI實(shí)現(xiàn)點(diǎn)擊左右箭頭切換頭像功能(類似輪播圖效果)
這篇文章主要介紹了vue.js+elementUI實(shí)現(xiàn)點(diǎn)擊左右箭頭切換頭像功能(類似輪播圖),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
淺談vue中computed屬性對(duì)data屬性賦值為undefined的原因
本文主要介紹了淺談vue中computed屬性對(duì)data屬性賦值為undefined的原因,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
electron實(shí)現(xiàn)靜默打印的示例代碼
這篇文章主要介紹了electron實(shí)現(xiàn)靜默打印的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
關(guān)于vue中@click.native.prevent的說(shuō)明
這篇文章主要介紹了關(guān)于vue中@click.native.prevent的說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Vue3之組件狀態(tài)保持KeepAlive的簡(jiǎn)單使用
這篇文章主要介紹了Vue3之組件狀態(tài)保持KeepAlive的簡(jiǎn)單使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05

