1. 程式人生 > >webpack+vue-cli中代理配置(proxyTable)

webpack+vue-cli中代理配置(proxyTable)

info ubd 技術 ets conf local src nod mage

在做vue的項目時,用到webpack打包工具,我們會發現本地開發開啟的node服務地址請求接口的地址存在跨域問題。本地開啟的服務地址是

http://localhost:8080

而服務器的地址是

http://192.168.28.92:9096/api/

如果在你的axios請求中直接寫服務器的地址進行請求,就會發生跨域的問題。如下

技術分享圖片

技術分享圖片

這就需要webpack中的proxytable代理進行配置,來解決這個跨域的問題。

在config/index.js文件中:

dev: {

    // Paths
    assetsSubDirectory: ‘static‘,
    assetsPublicPath: 
‘/‘, proxyTable: { ‘/api‘:{ target: ‘http://192.168.28.92:9096‘,
     // secure: false,// 如果是https接口,需要配置這個參數 changeOrigin:
true,//接口跨域,則需要進行這個參數配置
     //pathRewrite: {
     // ‘^api‘: ‘‘ //如果接口本身沒有/api,則需要通過pathRewrite來重寫
     //} } }, }

當你的服務器接口地址是:

http://120.79.61.208:36912/shop/

此時接口地址中沒有/api

則需要通過pathRewrite對接口進行重寫

proxyTable: {
      ‘/api‘:{
        target: ‘http://192.168.28.92:9096‘,
     // secure: false,// 如果是https接口,需要配置這個參數
        changeOrigin: true,//接口跨域,則需要進行這個參數配置
     pathRewrite: {
      ‘^api‘: ‘‘ //如果接口本身沒有/api,則需要通過pathRewrite來重寫
     }
      }

則在axios請求中的url:

技術分享圖片

webpack+vue-cli中代理配置(proxyTable)