vue cli4.0項目引入typescript的方法
現(xiàn)有的項目是采用vue cli4.0腳手架生成的,現(xiàn)在想要引入typescript。
1.執(zhí)行安裝命令
npm install --save-dev typescript npm install --save-dev @vue/cli-plugin-typescript
2.根目錄下新建 tsconfig.json
{ "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"] }
3.新增 shims-vue.d.ts
根目錄下新建 shims-vue.d.ts,讓 ts 識別 *.vue 文件,文件內(nèi)容如下:
declare module '*.vue' { import Vue from 'vue'; export default Vue; }
4.修改入口文件后綴
src/main.js => src/main.ts
5.改造 .vue 文件
src/main.js => src/main.ts
加上 lang=ts 可以讓webpack識別此段代碼為 typescript
6.使用裝飾器插件
vue-class-component:強化 Vue 組件,使用 TypeScript裝飾器 增強 Vue 組件,使得組件更加扁平化
vue-property-decorator:在 vue-class-component 上增強更多的結(jié)合 Vue 特性的裝飾
Demo:
import { Vue, Component ,Watch} from 'vue-property-decorator'; @Component({ components: { Loading } }) export default class App extends Vue{ old_back:object=null, transitionName:string = "slide-right"; get ...mapState("base", ["loadingStatus"]); @Watch('$route') onChangeValue(to:object, from:object){ // console.log('$route', to, from) const noBack = to.meta.noBack; // 監(jiān)聽路由變化時的狀態(tài)為前進還是后退 // 去終節(jié)點左,從終節(jié)點過來右 if (to.meta.last) { this.transitionName = "slide-left"; } else if (from.meta.last) { this.transitionName = "slide-right"; } else if (from.meta.leaf) { // 從其它葉子頁面過來的,往右 this.transitionName = "slide-right"; } else { if (noBack) { // 去到不需要返回的界面往右 this.transitionName = "slide-right"; } else { this.transitionName = "slide-left"; } } } @Watch('loadingStatus') onChangeValue(newVal: string){ if (newVal) { setTimeout(_ => { this.setLoading(false); }, 1500); } } // 彈出系統(tǒng)提示對話框 showAlert(msg:string) { plus.nativeUI.alert( msg, function() { // console.log("User pressed!"); }, "報警詳情", "確定" ); } }
到此這篇關(guān)于vue cli4.0項目引入typescript的文章就介紹到這了,更多相關(guān)vue cli4.0引入typescript內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2移動端使用vue-qrcode-reader實現(xiàn)掃一掃功能的步驟
最近在使用vue開發(fā)的h5移動端想要實現(xiàn)一個調(diào)用攝像頭掃描二維碼的功能,所以下面這篇文章主要給大家介紹了關(guān)于vue2移動端使用vue-qrcode-reader實現(xiàn)掃一掃功能的相關(guān)資料,需要的朋友可以參考下2023-06-06vue-router跳轉(zhuǎn)時打開新頁面的兩種方法
這篇文章主要給大家介紹了關(guān)于vue-router跳轉(zhuǎn)時打開新頁面的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用vue-router具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07vue使用路由守衛(wèi)實現(xiàn)菜單的權(quán)限設(shè)置
我們使?vue-element-admin前端框架開發(fā)后臺管理系統(tǒng)時,?般都會涉及到菜單的權(quán)限控制問題,下面這篇文章主要給大家介紹了關(guān)于vue使用路由守衛(wèi)實現(xiàn)菜單的權(quán)限設(shè)置的相關(guān)資料,需要的朋友可以參考下2023-06-06Vite配置優(yōu)雅的code?spliiting代碼分割詳解
這篇文章主要為大家介紹了Vite配置優(yōu)雅的code?spliiting代碼分割詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08Vue+Express實現(xiàn)登錄注銷功能的實例代碼
這篇文章主要介紹了Vue+Express實現(xiàn)登錄,注銷功能,本文通過實例代碼講解的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05