為何捐款
API 瀏覽器
升級指南
NEW!
quasar.config 檔案
轉換為搭配 Webpack 的 CLI
瀏覽器相容性
支援 TypeScript
目錄結構
指令列表
CSS 預處理器
路由
延遲載入 - 代碼分割
處理資源
啟動檔案
預取功能
API 代理
處理 Webpack
處理 process.env
搭配 Pinia 的狀態管理
搭配 Vuex 的狀態管理
Linter
測試與稽核
開發行動應用程式
Ajax 請求
對外公開開發伺服器
Quasar CLI 搭配 Webpack - @quasar/app-webpack
支援 TypeScript
@quasar/app-webpack v4+

預設情況下,Typescript 支援不會新增到您的專案中 (除非您在建立專案資料夾時選擇了 TS),但可以透過遵循本頁的指南輕鬆整合。

小提示

只有在您建立新的 Quasar 專案時沒有選擇 TypeScript 支援時,才需要執行以下步驟。如果您在專案建立時選擇了 TS 選項,則 TypeScript 支援已啟用。

安裝 TypeScript 支援

為了支援 TypeScript,您需要變更 quasar.config 檔案的擴充功能:/quasar.config 檔案

import { configure } from "quasar/wrappers";

module.exports = configure((ctx) => {
  return {
    supportTS: true,
    // ...
  }
});

然後在您的專案根目錄中建立 /tsconfig.json 檔案,內容如下

{
  "extends": "@quasar/app-webpack/tsconfig-preset",
  "compilerOptions": {
    // `baseUrl` should be set to the current folder to allow Quasar TypeScript preset to manage paths on your behalf
    "baseUrl": "."
  },
  "exclude": [
    "./dist",
    "./.quasar",
    "./node_modules",
    "./src-capacitor",
    "./src-cordova",
    "./quasar.config.*.temporary.compiled*"
  ]
}

現在您可以開始在您的專案中使用 TypeScript 了。

小提示

請記住,您必須將 JavaScript 檔案的擴充功能變更為 .ts,才能在其中寫入 TypeScript 程式碼。若要在您的元件中寫入 TS 程式碼,請將 script 開啟標籤變更為 <script lang="ts">

警告

如果您未能新增 tsconfig.json 檔案,應用程式將在編譯時中斷!

處理 TS Webpack 加載器
@quasar/app-webpack =v3

在幕後,Quasar 使用 ts-loaderfork-ts-checker-webpack-plugin (由 @quasar/app-webpack 套件提供) 來管理 TS 檔案。如果您需要為這些程式庫提供自訂設定,您可以透過建立如下的 build 屬性來完成

/quasar.config 檔案

module.exports = function (ctx) {
  return {
    supportTS: {
      tsLoaderConfig: {
        // `appendTsSuffixTo: [/\.vue$/]` and `transpileOnly: true` are added by default and cannot be overridden
        ...
      },
      tsCheckerConfig: {
        // `vue: true` is added by default and cannot be overridden
        ...
      }
    },
    ....
  }
}

Linting 設定

首先新增所需的依賴項


$ yarn add --dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
# you might also want to install the `eslint-plugin-vue` package.

然後相應地更新您的 ESLint 設定,如下列範例所示

/.eslintrc.cjs

const { resolve } = require('node:path');

module.exports = {
  // https://eslint.dev.org.tw/docs/user-guide/configuring#configuration-cascading-and-hierarchy
  // This option interrupts the configuration hierarchy at this file
  // Remove this if you have an higher level ESLint config file (it usually happens into a monorepos)
  root: true,

  // https://eslint.vuejs.org/user-guide/#how-to-use-custom-parser
  // Must use parserOptions instead of "parser" to allow vue-eslint-parser to keep working
  // `parser: 'vue-eslint-parser'` is already included with any 'plugin:vue/**' config and should be omitted
  parserOptions: {
    // https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser#configuration
    // https://github.com/TypeStrong/fork-ts-checker-webpack-plugin#eslint
    // Needed to make the parser take into account 'vue' files
    extraFileExtensions: ['.vue'],
    parser: '@typescript-eslint/parser',
    project: resolve(__dirname, './tsconfig.json'),
    tsconfigRootDir: __dirname,
    ecmaVersion: 2021, // Allows for the parsing of modern ECMAScript features
    sourceType: 'module', // Allows for the use of imports
  },

  // Rules order is important, please avoid shuffling them
  extends: [
    // Base ESLint recommended rules
    'eslint:recommended',

    // https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#usage
    // ESLint typescript rules
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
    // consider disabling this class of rules if linting takes too long
    'plugin:@typescript-eslint/recommended-requiring-type-checking',

    // https://eslint.vuejs.org/rules/#priority-a-essential-error-prevention
    // consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules
    'plugin:vue/essential',

    // --- ONLY WHEN USING PRETTIER ---
    // https://github.com/prettier/eslint-config-prettier#installation
    // usage with Prettier, provided by 'eslint-config-prettier'.
    'prettier',
    'prettier/@typescript-eslint',
    'prettier/vue',
  ],

  plugins: [
    // required to apply rules which need type information
    '@typescript-eslint',

    // https://eslint.vuejs.org/user-guide/#why-doesn-t-it-work-on-vue-file
    // required to lint *.vue files
    'vue',
  ],

  // add your custom rules here
  rules: {
    // others rules...

    // TypeScript
    'quotes': ['warn', 'single'],
    '@typescript-eslint/explicit-function-return-type': 'off',
  }
}

如果發生任何錯誤,請閱讀 typescript-eslint 指南,本範例即以此為基礎。

最後一步,更新您的 yarn lint 命令,使其也能 lint .ts 檔案。

小提示

TypeScript Linting 由於類型檢查的額外負擔而非常緩慢,我們建議您在開發建置中停用 /quasar.config 檔案中的 Webpack lint 擴充功能。

如果您設定了 TypeScript linting,並且希望 fork-ts-checker-webpack-plugin (由 @quasar/app-webpack 套件提供) 將其考慮在內,那麼您應該使用 tsCheckerConfig 屬性

/quasar.config 檔案

module.exports = function (ctx) {
  return {
    supportTS: {
      tsCheckerConfig: {
        eslint: {
          enabled: true,
          files: './src/**/*.{ts,tsx,js,jsx,vue}'
        }
      }
    },
    ....
  }
}