vue cli4.0項(xiàng)目引入typescript的方法
現(xiàn)有的項(xiàng)目是采用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 識(shí)別 *.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識(shí)別此段代碼為 typescript
6.使用裝飾器插件
vue-class-component:強(qiáng)化 Vue 組件,使用 TypeScript裝飾器 增強(qiáng) Vue 組件,使得組件更加扁平化
vue-property-decorator:在 vue-class-component 上增強(qiáng)更多的結(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)聽路由變化時(shí)的狀態(tài)為前進(jìn)還是后退
// 去終節(jié)點(diǎn)左,從終節(jié)點(diǎn)過來右
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)提示對(duì)話框
showAlert(msg:string) {
plus.nativeUI.alert(
msg,
function() {
// console.log("User pressed!");
},
"報(bào)警詳情",
"確定"
);
}
}
到此這篇關(guān)于vue cli4.0項(xiàng)目引入typescript的文章就介紹到這了,更多相關(guān)vue cli4.0引入typescript內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue 項(xiàng)目引入echarts 添加點(diǎn)擊事件操作
- 淺談在vue-cli3項(xiàng)目中解決動(dòng)態(tài)引入圖片img404的問題
- laravel+vue組合的項(xiàng)目中引入ueditor方式(打包成組件形式)
- Vue-cli3項(xiàng)目引入Typescript的實(shí)現(xiàn)方法
- vue項(xiàng)目中引入vue-datepicker插件的詳解
- vue學(xué)習(xí)筆記五:在vue項(xiàng)目里面使用引入公共方法詳解
- Vant的安裝和配合引入Vue.js項(xiàng)目里的方法步驟
- Vue項(xiàng)目如何引入bootstrap、elementUI、echarts
相關(guān)文章
vue2移動(dòng)端使用vue-qrcode-reader實(shí)現(xiàn)掃一掃功能的步驟
最近在使用vue開發(fā)的h5移動(dòng)端想要實(shí)現(xiàn)一個(gè)調(diào)用攝像頭掃描二維碼的功能,所以下面這篇文章主要給大家介紹了關(guān)于vue2移動(dòng)端使用vue-qrcode-reader實(shí)現(xiàn)掃一掃功能的相關(guān)資料,需要的朋友可以參考下2023-06-06
vue-router跳轉(zhuǎn)時(shí)打開新頁面的兩種方法
這篇文章主要給大家介紹了關(guān)于vue-router跳轉(zhuǎn)時(shí)打開新頁面的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue-router具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
vue使用路由守衛(wèi)實(shí)現(xiàn)菜單的權(quán)限設(shè)置
我們使?vue-element-admin前端框架開發(fā)后臺(tái)管理系統(tǒng)時(shí),?般都會(huì)涉及到菜單的權(quán)限控制問題,下面這篇文章主要給大家介紹了關(guān)于vue使用路由守衛(wèi)實(shí)現(xiàn)菜單的權(quán)限設(shè)置的相關(guān)資料,需要的朋友可以參考下2023-06-06
vue2?對(duì)全局自定義指令一次性進(jìn)行注冊(cè)的方法
這篇文章主要介紹了vue2?對(duì)全局自定義指令一次性進(jìn)行注冊(cè),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
Vite配置優(yōu)雅的code?spliiting代碼分割詳解
這篇文章主要為大家介紹了Vite配置優(yōu)雅的code?spliiting代碼分割詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Vue+Express實(shí)現(xiàn)登錄注銷功能的實(shí)例代碼
這篇文章主要介紹了Vue+Express實(shí)現(xiàn)登錄,注銷功能,本文通過實(shí)例代碼講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05

