為什麼捐款
API 瀏覽器
升級指南
NEW!
quasar.config 檔案
將專案轉換為搭配 Vite 的 CLI
瀏覽器相容性
支援 TypeScript
目錄結構
命令列表
CSS 預處理器
路由
延遲載入 - 程式碼分割
處理資源
啟動檔案
預先載入功能
API 代理
處理 Vite
處理 process.env
使用 Pinia 進行狀態管理
使用 Vuex 進行狀態管理
Linter
測試與稽核
開發行動應用程式
Ajax 請求
開放開發伺服器給公眾
Quasar CLI with Vite - @quasar/app-vite
開發環境的 API 代理

當把 Quasar CLI 建立的專案資料夾與現有的後端整合時,常見的需求是在使用開發伺服器時存取後端 API。為了達成這個目的,我們可以並排 (或遠端) 執行開發伺服器和 API 後端,並讓開發伺服器將所有 API 請求代理到實際的後端。

如果您在 API 請求中存取相對路徑,這會很有用。顯然地,這些相對路徑在您開發時可能無法運作。為了建立一個類似於您已部署的網站/應用程式所使用的環境,您可以代理您的 API 請求。

若要配置代理規則,請編輯 /quasar.config 檔案中的 devServer.proxy。底層使用了 http-proxy。完整選項列表請參閱此處

/quasar.config 檔案

devServer: {
  proxy: {
    // string shorthand
    '/foo': 'https://127.0.0.1:4567',
    // with options
    '/api': {
      target: 'http://jsonplaceholder.typicode.com',
      changeOrigin: true,
      rewrite: (path) => path.replace(/^\/api/, '')
    },
    // with RegEx
    '^/fallback/.*': {
      target: 'http://jsonplaceholder.typicode.com',
      changeOrigin: true,
      rewrite: (path) => path.replace(/^\/fallback/, '')
    },
    // Using the proxy instance
    '/api': {
      target: 'http://jsonplaceholder.typicode.com',
      changeOrigin: true,
      configure: (proxy, options) => {
        // proxy will be an instance of 'http-proxy'
      }
    },
    // Proxying websockets or socket.io
    '/socket.io': {
      target: 'ws://127.0.0.1:3000',
      ws: true
    }
  }
}