在Vue項目中使用Typescript的實現(xiàn)
3.0遲遲沒有發(fā)布release版本,現(xiàn)階段在Vue項目中使用Typescript需要花不小的精力在工程的配置上面。主要的工作是webpack對TS,TSX的處理,以及2.x版本下面使用class的形式書寫Vue 組件的一些限制和注意事項。
Webpack 配置
配置webpack對TS,TSX的支持,以便于我們在Vue項目中使用Typescript和tsx。
module.exports = { entry: './index.vue', output: { filename: 'bundle.js' }, resolve: { extensions: ['.ts', '.tsx', '.vue', '.vuex'] }, module: { rules: [ { test: /\.vue$/, loader: 'vue-loader', options: { loaders: { ts: 'ts-loader', tsx: 'babel-loader!ts-loader', } } }, { test: /\.ts$/, loader: 'ts-loader', options: { appendTsSuffixTo: [/TS\.vue$/] } }, { test: /\.tsx$/, loader: 'babel-loader!ts-loader', options: { appendTsxSuffixTo: [/TSX\.vue$/] } } ] } }
在上面的配置中,vue文件中的TS內(nèi)容將會使用ts-loader處理,而TSX內(nèi)容將會按照ts-loader-->babel-loader的順序處理。
appendTsSuffixTo/appendTsxSuffixTo 配置項的意思是說,從vue文件里面分離的script的ts,tsx(取決于<script lang="xxx"></script>)內(nèi)容將會被加上ts或者tsx的后綴,然后交由ts-loader解析。
我在翻看了ts-loader上關(guān)于appendTsxSuffixTo的討論發(fā)現(xiàn),ts-loader貌似對文件后綴名稱有很嚴格的限定,必須得是ts/tsx后綴,所以得在vue-loader extract <script>中內(nèi)容后,給其加上ts/tsx的后綴名,這樣ts-loader才會去處理這部分的內(nèi)容。
ts-loader只對tsx做語法類型檢查,真正的jsx-->render函數(shù)應(yīng)該交由babel處理。
所以我們還需要使用plugin-transform-vue-jsx來將vue jsx轉(zhuǎn)換為真正的render函數(shù)。
// babel.config.json { "presets": ["env"], "plugins": ["transform-vue-jsx"] }
同時,配置TS對tsx的處理為preserve,讓其只對tsx做type類型檢查。
// tsconfig.json { "compilerOptions": { "jsx": "preserve", }
使用vue cli 4.x
高版本的vue cli如4.x已經(jīng)集成了vue + typescript的配置。選擇use Typescript + Use class-style component syntax選項創(chuàng)建工程。
創(chuàng)建后的工程目錄如下:
在src根目錄下,有兩個shims.xx.d.ts的類型聲明文件。
// shims.vue.d.ts declare module "*.vue" { import Vue from "vue"; export default Vue; }
// shims.jsx.d.ts import Vue, { VNode } from "vue"; declare global { namespace JSX { // tslint:disable no-empty-interface interface Element extends VNode {} // tslint:disable no-empty-interface interface ElementClass extends Vue {} interface IntrinsicElements { [elem: string]: any; } } }
它們是作什么用的呢?
shims.vue.d.ts給所有.vue文件導(dǎo)出的模塊聲明了類型為Vue,它可以幫助IDE判斷.vue文件的類型。
shims.jsx.d.ts 為 JSX 語法的全局命名空間,這是因為基于值的元素會簡單的在它所在的作用域里按標識符查找。當在 tsconfig 內(nèi)開啟了 jsx 語法支持后,其會自動識別對應(yīng)的 .tsx 結(jié)尾的文件,(也就是Vue 單文件組件中<script lang="tsx"></script>的部分)可參考
官網(wǎng) tsx
基本用法
在vue 2.x中使用class的方式書寫vue組件需要依靠vue-property-decorator來對vue class做轉(zhuǎn)換。
<script lang="ts"> import { Component, Prop, Vue } from "vue-property-decorator"; export default class extends Vue { @Prop({ default: 'default msg'}) private msg!: string; name!: string; show() { console.log("this.name", this.name); } } </script>
導(dǎo)出的class是經(jīng)過Vue.extend之后的VueComponent函數(shù)(理論上class就是一個Function)。
其最后的結(jié)果就像我們使用Vue.extend來擴展一個Vue組件一樣。
// 創(chuàng)建構(gòu)造器 var Profile = Vue.extend({ template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>', data: function () { return { firstName: 'Walter', lastName: 'White', alias: 'Heisenberg' } } }) export default { components: { Profile } }
注意上面的Profile組件并不是和我們平時一樣寫的Vue組件是一個plain object配置對象,它其實是一個VueComponent函數(shù)。
父組件實例化子組件的時候,會對傳入的vue object 進行擴展,使用Vux.extend轉(zhuǎn)換為組件函數(shù)。
如果components中的值本身是一個函數(shù),就會省略這一步。這一點, 從Vue 源碼中可以看出。
if (isObject(Ctor)) { Ctor = baseCtor.extend(Ctor) }
上面的Ctor就是在components中傳入的組件,對應(yīng)于上面導(dǎo)出的Profile組件。
使用vuex
使用vuex-class中的裝飾器來對類的屬性做注解。
import Vue from 'vue'import Component from 'vue-class-component'import { State, Getter, Action, Mutation, namespace } from 'vuex-class' const someModule = namespace('path/to/module') @Component export class MyComp extends Vue { @State('foo') stateFoo @State(state => state.bar) stateBar @Getter('foo') getterFoo @Action('foo') actionFoo @Mutation('foo') mutationFoo @someModule.Getter('foo') moduleGetterFoo // If the argument is omitted, use the property name // for each state/getter/action/mutation type @State foo @Getter bar @Action baz @Mutation qux created () { this.stateFoo // -> store.state.foo this.stateBar // -> store.state.bar this.getterFoo // -> store.getters.foo this.actionFoo({ value: true }) // -> store.dispatch('foo', { value: true }) this.mutationFoo({ value: true }) // -> store.commit('foo', { value: true }) this.moduleGetterFoo // -> store.getters['path/to/module/foo'] } }
mixin
對于mixin,我們使用class的繼承很容易實現(xiàn)類似功能。
import Vue from 'vue' import { Component } from 'vue-property-decorator' @Component class DeployMixin extends Vue{ name: string; deploy(){ // do something } } @Component class Index extends DeployMixin{ constructor(){ super() } sure(){ this.deploy() } }
VS code jsx快捷鍵
設(shè)置 VS code中對emmet的支持
"emmet.includeLanguages": { "javascript": "html" }
或者是
"emmet.includeLanguages": { "javascript": "javascriptreact" }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
欄 目:JavaScript
本文標題:在Vue項目中使用Typescript的實現(xiàn)
本文地址:http://mengdiqiu.com.cn/a1/JavaScript/9474.html
您可能感興趣的文章
- 01-10使用webpack/gulp構(gòu)建TypeScript項目的方法示例
- 01-10Vue中使用Lodop插件實現(xiàn)打印功能的簡單方法
- 01-10Vue filter 過濾當前時間 實現(xiàn)實時更新效果
- 01-10Vuex實現(xiàn)數(shù)據(jù)共享的方法
- 01-10Vue+Node服務(wù)器查詢Mongo數(shù)據(jù)庫及頁面數(shù)據(jù)傳遞操作實例分析
- 01-10推薦幾個不錯的console調(diào)試技巧實現(xiàn)
- 01-10vue中根據(jù)時間戳判斷對應(yīng)的時間(今天 昨天 前天)
- 01-10Vue+Node實現(xiàn)的商城用戶管理功能示例
- 01-10vue實現(xiàn)拖拽效果
- 01-10vue圖片上傳組件使用詳解


閱讀排行
本欄相關(guān)
- 04-02javascript點線,點線的代碼
- 04-02javascript潛力,javascript強大嗎
- 04-02javascript替換字符串,js字符串的替換
- 04-02javascript移出,js 移入移出
- 04-02包含javascript舍的詞條
- 04-02javascript并行,深入理解并行編程 豆瓣
- 04-02javascript匿名,js匿名方法
- 04-02javascript警報,JavaScript警告
- 04-02javascript遮蓋,JavaScript遮蓋PC端頁面
- 04-02javascript前身,javascript的前身
隨機閱讀
- 01-10C#中split用法實例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法