vue-cli專案中的跨域問題,設定訪問代理http/https
阿新 • • 發佈:2018-12-25
找到專案中的config資料夾下的index.js檔案,dev物件內有一個proxyTable的配置項。
dev: { // Paths assetsSubDirectory: "static", assetsPublicPath: "/", proxyTable: { "/dev": { target: "http://10.78.8.136:81", //設定你呼叫的介面域名和埠號 別忘了加http changeOrigin: true, pathRewrite: { "^/dev": "" //這裡理解成用‘/dev'代替target裡面的地址,後面元件中我們掉介面時直接用dev代替 //比如我要呼叫'http://10.78.8.136:8081/user/login',直接寫‘/dev/user/login'即可 } } }, // Various Dev Server settings // host: '10.78.10.64', // can be overwritten by process.env.HOST host: "0.0.0.0", // can be overwritten by process.env.HOST port: 6207, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined autoOpenBrowser: false, errorOverlay: false, notifyOnErrors: false, poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions- /** * Source Maps */ // https://webpack.js.org/configuration/devtool/#development devtool: "cheap-module-eval-source-map", // If you have problems debugging vue-files in devtools, // set this to false - it *may* help // https://vue-loader.vuejs.org/en/options.html#cachebusting cacheBusting: true, cssSourceMap: true }
- 在config/index.js配置完之後,在build/webpack.dev.conf.js中的dev-Server中的proxy中進行呼叫
這樣之後就可以使用啦,但是設定的這個代理只能在開發環境內才可以使用。
webpack預設代理不支援 https 協議,如若要代理https請求,則需配置如下,
新增 secure:false
vue-cli的這個設定來自於其使用的外掛http-proxy-middleware