?最近想學習一下TypeScript語法,但是只是看官方檔案又有些乏味,還是通過專案在實踐中學習比較有趣,所以在這里記錄一下我的學習歷程,與Vue專案結合開發,(官方檔案 請戳 >>)
?
專案搭建
通過腳手架搭建
1. 通過Vue CLI 3 創建vue專案
vue create vue-typescript
// 在此選擇typescript支持
? Check the features needed for your project: () Babel () TypeScript ( ) Progressive Web App (PWA) Support () Router () Vuex >() CSS Pre-processors () Linter / Formatter ( ) Unit Testing ( ) E2E Testing 我是08年出道的高級前端架構師,有問題或者交流經驗可以進我的扣扣裙 519293536 我都會盡力幫大家哦
復制代碼
// 引入 vue-class-component 插件 // 以下大概是我的選擇 ? Use class-style component syntax? (Y/n) y ? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? Yes ? Use history mode for router? (Requires proper server setup for index fallback in production) Yes ? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with node-sass) ? Pick a linter / formatter config: Prettier ? Pick additional lint features: Lint on save ? Where do you prefer placing config for Babel, ESLint, etc.? In dedicated config files ? Save this as a preset for future projects? (y/N) n
復制代碼
2. 啟動專案
yarn run serve
復制代碼
能跑起來嗎,能跑就是好專案??
3.專案配置
此時其實腳手架已經幫我們配置好了大多數的配置,但還是需要熟悉一下配置,
tsconfig.json
在專案根目錄下創建tsconfig.json,
{
// 編譯選項
"compilerOptions": {
// 輸出目錄
"outDir": "./output",
// 是否包含可以用于 debug 的 sourceMap
"sourceMap": true,
// 以嚴格模式決議
"strict": true,
// 采用的模塊系統
"module": "esnext",
// 如何處理模塊
"moduleResolution": "node",
// 編譯輸出目標 ES 版本
"target": "es5",
// 允許從沒有設定默認匯出的模塊中默認匯入
"allowSyntheticDefaultImports": true,
// 將每個檔案作為單獨的模塊
"isolatedModules": false,
// 啟用裝飾器
"experimentalDecorators": true,
// 啟用設計型別元資料(用于反射)
"emitDecoratorMetadata": true,
// 在運算式和宣告上有隱含的any型別時報錯
"noImplicitAny": false,
// 不是函式的所有回傳路徑都有回傳值時報錯,
"noImplicitReturns": true,
// 從 tslib 匯入外部幫助庫: 比如__extends,__rest等
"importHelpers": true,
// 編譯程序中列印檔案名
"listFiles": true,
// 移除注釋
"removeComments": true,
"suppressImplicitAnyIndexErrors": true,
// 允許編譯javascript檔案
"allowJs": true,
// 決議非相對模塊名的基準目錄
"baseUrl": "./",
// 指定特殊模塊的路徑
"paths": {
"jquery": [
"node_modules/jquery/dist/jquery"
]
},
// 編譯程序中需要引入的庫檔案的串列
"lib": [
"dom",
"es2015",
"es2015.promise"
]
}
}
復制代碼
tslint.json
在根路徑下創建tslint.json檔案,就是 引入 ts 的 standard 規范,
如果已經引入了eslint的組態檔,這一步其實也可以不做,
{
"extends": "tslint-config-standard",
"globals": {
"require": true
}
}
復制代碼
附上一個腳手架自動生成的eslint的默認配置 eslintrc.js,
module.exports = {
root: true,
env: {
node: true
},
extends: [
"plugin:vue/essential",
"eslint:recommended",
"@vue/typescript/recommended",
"@vue/prettier",
"@vue/prettier/@typescript-eslint"
],
parserOptions: {
ecmaVersion: 2020
},
rules: {
"no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
"no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off"
}
};
復制代碼
4.支持ES6 / ES7
在 tsconfig.json中,添加對es6 / es7的支持,更多的配置請見tsconfig - 編譯選項,
"lib": [
"dom",
"es5",
"es6",
"es7",
"es2015.promise"
]
復制代碼
5.配置Vuex
首先當然是先安裝依賴啦,
yarn add vuex vuex-class
復制代碼
- vuex:在
vue中集中管理應用狀態 - vuex-class :在
vue-class-component寫法中 系結vuex,
參考了vuex-class之后還是和原來的寫法有點兒區別的,
vuex store 中該怎么寫,還怎么寫,參考的時候如下:
import { Component, Prop, Vue } from "vue-property-decorator";
import { State, Getter, Action, Mutation, namespace } from "vuex-class";
// 宣告使用的是哪個模塊
const commonModule = namespace("common");
@Component
export default class HelloWorld extends Vue {
@Prop() private msg!: string;
// 獲取vuex中的資料
@commonModule.State("token") token!: string;
@commonModule.Getter("getToken") getToken!: string;
@commonModule.Action("setToken") actionSetToken: (arg0: string) => void;
@commonModule.Mutation("setToken") mutationSetToken: unknown;
// @State token
// @Getter("token") getterToken;
// @Action("token") actionToken;
// @Mutation("token") mutationToken;
created() {
this.actionSetToken("123");
alert(this.token);
}
}
復制代碼
6.支持 vue mixin 函式
在src下新建mixins目錄,根據業務復用模塊劃分結構,
在使用Vue進行開發時我們經常要用到混合,結合TypeScript之后一般有兩種mixins的方法,
一種是vue-property-decorator提供的
// 定義 routerMixins.ts檔案
// mixin router 公共方法
import Vue from 'vue'
import Component from 'vue-class-component'
@Component
export default class myMixins extends Vue {
/**
* @author: WangXinYu
* @describe: 瀏覽器后退事件
* @param: {}
* @return:
**/
goBack() {
this.$router.go(-1)
}
/**
* @author: WangXinYu
* @describe: test
* @param: {}
* @return:
**/
routerTest() {
console.log('are you ok?')
}
}
復制代碼
// 引入 mixin
import { Component, Prop, Vue, Mixins } from 'vue-property-decorator'
import routerMixins from '@/mixins/router'
@Component
export default class HelloWorld extends Mixins(routerMixins) {
created() {
this.routerTest()
}
}
復制代碼
第二種是在@Component中混入,
// mixins.ts
import { Vue, Component } from 'vue-property-decorator';
declare module 'vue/types/vue' {
interface Vue {
value: string;
}
}
@Component
export default class myMixins extends Vue {
value: string = 'Hello'
}
復制代碼
// 混入
import { Vue, Component, Prop } from 'vue-property-decorator';
import myMixins from '@/mixins/myMixins';
@Component({
mixins: [myMixins]
})
export default class myComponent extends Vue{
created(){
console.log(this.value) // => Hello
}
}
都會了嗎?如果不懂 ,記住我 ,我是08年出道的高級前端架構師,有問題或者交流經驗可以進我的扣扣裙 519293536 我都會盡力幫大家哦
本文的文字及圖片來源于網路加上自己的想法,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理
(未完待續...)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/67752.html
標籤:JavaScript
上一篇:教你如何用24個ES6方法解決實際開發的JS問題?本文詳解
下一篇:函式內賦值失效問題
