Vue-cli3項(xiàng)目引入Typescript的實(shí)現(xiàn)方法
前言
假設(shè)已經(jīng)有一個(gè)通過 vue-cli3 腳手架構(gòu)建的 vue 項(xiàng)目
命令行安裝 Typescript
npm install --save-dev typescript npm install --save-dev @vue/cli-plugin-typescript
編寫 Typescript 配置
根目錄下新建 tsconfig.json,下面為一份配置實(shí)例(點(diǎn)擊查看所有配置項(xiàng))。值得注意的是,默認(rèn)情況下,ts 只負(fù)責(zé)靜態(tài)檢查,即使遇到了錯(cuò)誤,也僅僅在編譯時(shí)報(bào)錯(cuò),并不會(huì)中斷編譯,最終還是會(huì)生成一份 js 文件。如果想要在報(bào)錯(cuò)時(shí)終止 js 文件的生成,可以在 tsconfig.json 中配置 noEmitOnError 為 true。
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"allowJs": false,
"noEmit": true,
"types": [
"webpack-env"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"exclude": [
"node_modules"
]
}
新增 shims-vue.d.ts
根目錄下新建 shims-vue.d.ts,讓 ts 識(shí)別 *.vue 文件,文件內(nèi)容如下
declare module '*.vue' {
import Vue from 'vue'
export default Vue
}
修改入口文件后綴
src/main.js => src/main.ts
改造 .vue 文件
.vue 中使用 ts 實(shí)例
// 加上 lang=ts 讓webpack識(shí)別此段代碼為 typescript
<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
// ...
})
</script>
一些好用的插件
vue-class-component:強(qiáng)化 Vue 組件,使用 TypeScript裝飾器 增強(qiáng) Vue 組件,使得組件更加扁平化。點(diǎn)擊查看更多
import Vue from 'vue'
import Component from 'vue-class-component'
// 表明此組件接受propMessage參數(shù)
@Component({
props: {
propMessage: String
}
})
export default class App extends Vue {
// 等價(jià)于 data() { return { msg: 'hello' } }
msg = 'hello';
// 等價(jià)于是 computed: { computedMsg() {} }
get computedMsg() {
return 'computed ' + this.msg
}
// 等價(jià)于 methods: { great() {} }
great() {
console.log(this.computedMsg())
}
}
vue-property-decorator:在 vue-class-component 上增強(qiáng)更多的結(jié)合 Vue 特性的裝飾。點(diǎn)擊查看更多
import { Vue, Component, Prop, Watch, Emit } from 'vue-property-decorator'
@Component
export default class App extends Vue {
@Prop(Number) readonly propA: Number | undefined
@Prop({ type: String, default: ''}) readonly propB: String
// 等價(jià)于 watch: { propA(val, oldval) { ... } }
@Watch('propA')
onPropAChanged(val: String, oldVal: String) {
// ...
}
// 等價(jià)于 resetCount() { ... this.$emit('reset') }
@Emit('reset')
resetCount() {
this.count = 0
}
// 等價(jià)于 returnValue() { this.$emit('return-value', 10, e) }
@Emit()
returnValue(e) {
return 10
}
// 等價(jià)于 promise() { ... promise.then(value => this.$emit('promise', value)) }
@Emit()
promise() {
return new Promise(resolve => {
resolve(20)
})
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue3+TypeScript實(shí)現(xiàn)遞歸菜單組件的完整實(shí)例
- vue中使用TypeScript的方法
- Vue新搭檔TypeScript快速入門實(shí)踐記錄
- Vue3+TypeScript封裝axios并進(jìn)行請求調(diào)用的實(shí)現(xiàn)
- 詳解Vue3.0 + TypeScript + Vite初體驗(yàn)
- vue3+typeScript穿梭框的實(shí)現(xiàn)示例
- vue3+typescript實(shí)現(xiàn)圖片懶加載插件
- Vue 使用typescript如何優(yōu)雅的調(diào)用swagger API
- 如何在Vue項(xiàng)目中應(yīng)用TypeScript類
相關(guān)文章
vue配置nprogress實(shí)現(xiàn)頁面頂部進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了vue配置nprogress實(shí)現(xiàn)頁面頂部進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
vue-auto-focus: 控制自動(dòng)聚焦行為的 vue 指令方法
今天小編就為大家分享一篇vue-auto-focus: 控制自動(dòng)聚焦行為的 vue 指令方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Vue+axios使用FormData方式向后端發(fā)送數(shù)據(jù)
在前后端分離的項(xiàng)目中經(jīng)常使用到Vue+axios通過FormData的方式向后端發(fā)送表單數(shù)據(jù),下面就來介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下2023-09-09
vue radio單選框,獲取當(dāng)前項(xiàng)(每一項(xiàng))的value值操作
這篇文章主要介紹了vue radio單選框,獲取當(dāng)前項(xiàng)(每一項(xiàng))的value值操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
如何解決Element-ui的el-table固定列后出現(xiàn)的表格錯(cuò)位問題
這篇文章主要介紹了如何解決Element-ui的el-table固定列后出現(xiàn)的表格錯(cuò)位問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-09-09

