在build 目錄的 webpack.dev.config.js 目錄中
module.exports = merge(webpackBaseConfig, { devtool: '#source-map', output: { publicPath: '/dist/', filename: '[name].js', chunkFilename: '[name].chunk.js' }, plugins: [ new ExtractTextPlugin({ filename: '[name].css', allChunks: true }), new webpack.optimize.CommonsChunkPlugin({ name: 'vendors', filename: 'vendors.js' }), new HtmlWebpackPlugin({ filename: '../index.html', template: './src/template/index.ejs', inject: false }) ], //設置跨域代理 devServer: { historyApiFallback: true, hot: true, inline: true, stats: { colors: true }, proxy: { //匹配代理的url '/api': { // 目標服務器地址 target: 'http://127.0.0.1:8081', //路徑重寫 pathRewrite: {'^/api' : '/api'}, changeOrigin: true } } }
請求時
//引入axios import axios from 'axios'; //請求方法,根據實際情況使用 axios.get('/api/user').then((res) => { //res 為成功回調的響應 console.log(res); });
配置后則可以將請求轉發到
http://127.0.0.1:8081
此處以本機啟動服務器為例
請根據具體情況自行更改
