用postman调接口有正确的返回值,与后台无关;肯定是前端配置问题;找了好几个小时。

1.main.js中使用到mock的信息注释掉,因为axios与mock会有冲突

 // import { mockXHR } from '../mock'

// if (process.env.NODE_ENV === 'production') {

// mockXHR()

// }

2.config下面的index.js中设置proxy代理的代码片段,设置之后要重新npm run dev,webstorm的自动重启不起作用

我的原因就是应为没有重启,/(ㄒoㄒ)/~~

 

在proxy模块中设置了‘/xxf’,target中设置服务器地址+端口号,然后我们在调用接口的时候,就可以全局使用‘/xxf’,这时候‘/xxf’的作用就相当于一个唯一标识,比如接口地址是 http://192.168.106.8:8888/xxf/deptList,那我们在调用接口时可以直接写为/xxf/deptList,系统会自动识别proxy中/xxf标识中的target地址并拼接在一起。

那pathRewrite是用来干嘛的呢,这里的作用,相当于是替换‘/xxf’,如果接口中没有/xxf,那就直接置空,{‘^/xxf’:‘’},置空后真实的地址就变成了 http://192.168.106.8:8888/xxf/deptList,与真实接口不一致,这就会导致了接口错误,报错404;

如果接口中有/xxf,那就得写成{‘^/xxf’:‘/xxf’},可以理解为一个替换路径、重写路径或者重定向的功能。

或者不设置pathRewrite参数

'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: {
      '/xxf':{
         //target: 'https://home.rootensoft.com/api/',
         target: 'http://localhost:8888',
        secure: true,
        changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
        pathRewrite: {   //路径重置
          '^/xxf': '/xxf'
        }
      }
    },

    host: '127.0.0.1', // can be overwritten by process.env.HOST
    port: 8889, // 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-

    useEslint: false,
    showEslintErrorsInOverlay: false,

    devtool: 'cheap-module-eval-source-map',

    cacheBusting: true,

    cssSourceMap: true
  },

  build: {
    index: path.resolve(__dirname, '../dist/index.html'),

    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: './',

    productionSourceMap: true,
    devtool: '#source-map',

    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],

    bundleAnalyzerReport: process.env.npm_config_report
  }
}

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐