vue-element-template 解决跨域问题

  1. 在mai.js中,去掉引入mockXHR
    1. /**
    2. * If you don't want to use mock-server
    3. * you want to use MockJs for mock api
    4. * you can execute: mockXHR()
    5. *
    6. * Currently MockJs will be used in the production environment,
    7. * please remove it before going online ! ! !
    8. */
    9. if (process.env.NODE_ENV === 'production') {
    10. const { mockXHR } = require('../mock')
    11. mockXHR()
    12. }
  1. 在.env.development中,把BASE_API设为/api。发布环境同理修改.env.production
    1. # base api
    2. VUE_APP_BASE_API = '/api'
  1. vue.config.js中,在devServer下添加proxy,并去掉引入mock-server.js
    1. devServer: {
    2. port: port,
    3. open: true,
    4. overlay: {
    5. warnings: false,
    6. errors: true
    7. },
    8. proxy: {
    9. '/api': {
    10. // 这里就改成自己的api路径
    11. target: 'http://localhost:8081',
    12. changeOrigin: true,
    13. pathRewrite: {
    14. '^/api': ''
    15. }
    16. }
    17. }
    18. // before: require('./mock/mock-server.js')
    19. }
  1. 该方法仅对dev环境有效