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)文章
關(guān)于element?ui的菜單default-active默認(rèn)選中的問題
這篇文章主要介紹了關(guān)于element?ui的菜單default-active默認(rèn)選中的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05vue如何利用axios調(diào)用后臺(tái)api接口
這篇文章主要介紹了vue如何利用axios調(diào)用后臺(tái)api接口問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07說說如何在Vue.js中實(shí)現(xiàn)數(shù)字輸入組件的方法
這篇文章主要介紹了說說如何在Vue.js中實(shí)現(xiàn)數(shù)字輸入組件的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01Vue 實(shí)例中使用$refs的注意事項(xiàng)
這篇文章主要介紹了Vue 實(shí)例中使用$refs的注意事項(xiàng),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號(hào))沖突問題
這篇文章主要介紹了解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號(hào))沖突問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09使用mint-ui實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)效果的示例代碼
下面小編就為大家分享一篇使用mint-ui實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)效果的示例代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02解決vue keep-alive 數(shù)據(jù)更新的問題
今天小編就為大家分享一篇解決vue keep-alive 數(shù)據(jù)更新的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09