欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue項目實現(xiàn)webpack配置代理,解決跨域問題

 更新時間:2022年04月09日 10:36:51   作者:歌頌大海  
這篇文章主要介紹了vue項目實現(xiàn)webpack配置代理,解決跨域問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

webpack配置代理,解決跨域

在config文件夾中的index.js文件配置

主要是這句話

proxyTable: { //本地測試接口
? ? ? '/': {
? ? ? ? ?target: 'http://xx.xx.xx.xx',
? ? ? ? ?changeOrigin: true,
? ? ? ? ?secure: false
? ? ?}
?},

?示例代碼:

module.exports = {
  dev: {
 
    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: { //本地測試接口
      '/': {
         target: 'http://xx.xx.xx.xx',
         changeOrigin: true,
         secure: false
       }
     },
 
    // 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- 
 
    /**
     * 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
  },
 
  build: {
    // Template for index.html
    index: path.resolve(__dirname, '../dist/index.html'),
 
    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
 
    /**
     * Source Maps
     */
 
    productionSourceMap: true,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',
 
    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],
 
    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  }
}
 
?

vue跨域問題,修改代理后仍404

首先確認(rèn)安裝了axios,安裝方法:cnpm install axios -S或者不用鏡像npm install axios

在這里插入圖片描述

dev: {undefined

// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
  '/api': {
    target: 'http://40.73.37.92:8090/',//設(shè)置你調(diào)用的接口域名和端口號 別忘了加http
    changeOrigin: true,
    pathRewrite: {
      '^/api': ''//這里理解成用‘/api'代替target里面的地址,后面組件中我們掉接口時直接用api代替 比如我要調(diào)用'http://40.00.100.100:3002/user/add',直接寫‘/api/user/add'即可
    }
  }
},

接口請求用法

在這里插入圖片描述

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論