在vue中使用typescript - 配置篇

以@vue/cli創(chuàng)建的項(xiàng)目

  1. 創(chuàng)建項(xiàng)目的時(shí)候, 選擇typescript, 會(huì)自動(dòng)添加好typescript
  2. 已有的項(xiàng)目添加typescript, 使用命令vue add typescript, 相關(guān)鏈接

自行配置webpack的項(xiàng)目

  1. npm下載依賴包
  • 安裝 typescript, ts-loader, tslint, tslint-loader, tslint-config-standard, vue-property-decorator
  1. 增加 tsconfig.json
  • vue/cli 中的配置
  {
    "compilerOptions": {
      "target": "esnext",
      "module": "esnext",
      "strict": true,
      "jsx": "preserve",
      "importHelpers": true,
      "moduleResolution": "node",
      "experimentalDecorators": true,
      "esModuleInterop": true,
      "allowSyntheticDefaultImports": true,
      "sourceMap": true,
      "baseUrl": ".",
      "types": [
        "webpack-env"
      ],
      "paths": {
        "@/*": [
          "src/*"
        ]
      },
      "lib": [
        "esnext",
        "dom",
        "dom.iterable",
        "scripthost"
      ]
    },
    "include": [
      "src/**/*.ts",
      "src/**/*.tsx",
      "src/**/*.vue",
      "tests/**/*.ts",
      "tests/**/*.tsx"
    ],
    "exclude": [
      "node_modules"
    ]
  }
  1. webpack.base.conf.js 中的配置
  resolve: {
      extensions: ['.js', '.vue', '.json', 'ts', 'tsx'], // 新增了'ts', 'tsx'
      alias: {
        'vue$': 'vue/dist/vue.esm.js',
        '@': resolve('src'),
      }
    },
    module: {
      rules: [
        {
          test: /\.ts$/,  // 用于加載項(xiàng)目中的ts文件
          exclude: /node_modules/,
          enforce: 'pre',
          loader: 'tslint-loader'
        },
        {
          test: /\.tsx?$/, // 用于加載項(xiàng)目中的tsx文件
          loader: 'ts-loader',
          exclude: /node_modules/,
          options: {
            appendTsSuffixTo: [/\.vue$/],
          }
        }]
  1. 重命名main.js
  • 將main.js重命名為main.ts
  • 在webpack.base.conf.js中修改入口的文件名 entry: {app: './src/main.ts'}
  1. 在main.ts同級(jí)目錄下添加shims-tsx.d.ts 和 shims-vue.d.ts
  // vue/cli中的shims-tsx.d.ts
  // 作用: 為 JSX 語法的全局命名空間
  // 這是因?yàn)榛谥档脑貢?huì)簡單的在它所在的作用域里按標(biāo)識(shí)符查找
  // 此處使用的是無狀態(tài)函數(shù)組件的方法來定義, 當(dāng)在tsconfig內(nèi)開啟了jsx語法支持后, 其會(huì)自動(dòng)識(shí)別對(duì)應(yīng)的.tsx結(jié)尾的文件
  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
      }
    }
  }

  // vue/cli中的shims-vue.d.ts
  // 作用:為項(xiàng)目內(nèi)所有的 vue 文件做模塊聲明, 畢竟 ts 默認(rèn)只識(shí)別 .d.ts、.ts、.tsx 后綴的文件
  import Vue from "vue";
  import VueRouter, { Route } from "vue-router";

  declare module '*.vue' {
    export default Vue
  }

  declare module "vue/types/vue" {
    interface Vue {
      $router: VueRouter; // 這表示this下有這個(gè)東西
      $route: Route;
      $https: any;
      $urls: any;
      $Message: any;
      $Modal: any;
    }
  }
  1. 接下來就可以開發(fā)了, 開發(fā)的時(shí)候依賴 vue-property-decorator
  • 提供了 Vue Component, Prop, PropSync, Model, Watch, Provide, Inject, ProvideReactive, InjectReactive, Emit, Ref API
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Vue-Webpack-TypeScript 這是一個(gè)Vue2.5+版本兼容TypeScript開發(fā)的腳手架,雖然...
    Jacky_MYD閱讀 2,860評(píng)論 0 14
  • 開發(fā)工具 vscode 眾所周知,vue2+Typescript的開發(fā)體驗(yàn)很不好,不過為了嘗鮮,咱還是可以搭一個(gè)小...
    北辰_狼月閱讀 1,523評(píng)論 1 11
  • 整理自:三命:Vue + TypeScript 新項(xiàng)目起手式最新版:Vue-cli 整合 Typescript 筆...
    六毫笙閱讀 2,106評(píng)論 1 2
  • 心理學(xué)家海因茨·科胡特說:“一個(gè)功能良好的心理結(jié)構(gòu),最重要的來源是父母的人格,特別是他們以不帶敵意的堅(jiān)決和不含誘惑...
    y詩淇閱讀 87評(píng)論 0 0
  • 生活在海邊的人,每天面對(duì)大海,不覺新鮮;生活在大山里的人,開門見山,平淡無奇。但是總會(huì)有人慕名而來,看山看海...
    正版云傾閱讀 221評(píng)論 0 2

友情鏈接更多精彩內(nèi)容