vue項目如何配置proxyTable解決跨域問題

  1. 在項目的開發之中,做數據請求的時候,會經常遇到跨域的問題。我們可以在config目錄下的index.js文件中,配置proxyTable去解決。

  2. /api 是匹配所有以'/api' 開頭的請求路徑,target是代理目標的基礎路徑,changeOrigin是支持跨域,pathRewrite是重寫路徑,去掉路徑中開頭的'/api',代碼如下所示:

const path = require('path')

module.exports = {
  dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {
        target: 'http://localhost:4000',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }
    ......
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章