Vue-本地跨域訪問後臺配置。
阿新 • • 發佈:2018-11-17
1.進入Vue專案的config/index.js 檔案。
未配置之前是這樣的:
module.exports = { dev: { // Paths assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: {}, // Various Dev Server settings host: 'localhost', // can be overwritten by process.env.HOST port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined autoOpenBrowser: false, errorOverlay: true, notifyOnErrors: true, poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
配置:注意在Vue專案中修改配置檔案之後一定要 npm run dev 重啟專案。
'use strict' // Template version: 1.3.1 // see http://vuejs-templates.github.io/webpack for documentation. const path = require('path') module.exports = { dev: { // Paths assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: { '/api':{ target: 'http://192.168.0.36:8080', //這裡面是你要訪問的IP地址 changeOrigin: true, //開啟代理 pathRewrite:{ '/api': '/api' } } }, // Various Dev Server settings host: '192.168.0.36', // can be overwritten by process.env.HOST port: 8081, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined autoOpenBrowser: false, errorOverlay: true, notifyOnErrors: true, poll: false,