根据nuxt官方文档提供的axios module

安装:

npm install @nuxtjs/axios @nuxtjs/proxy --save

 

nuxt.config.js

  modules: [
    '@nuxtjs/axios','@nuxtjs/proxy'
  ],
  axios: {
      proxy: true, // 表示开启代理
      prefix: '/api/channel', // 表示给请求url加个前缀 /api
      credentials: true // 表示跨域请求时是否需要使用凭证
  },
  proxy: {
    '/api': {
      target: 'http://47.94.142.215:8082', // 目标接口域名
      changeOrigin: true, // 表示是否跨域
      pathRewrite: {
        '^/api': '/', // 把 /api 替换成 /
      }
    }
  },
  build: {
    transpile: [/^element-ui/],

    /*
    ** You can extend webpack config here
    */
    extend(config, ctx) {
    },
    vendor: ['axios'] //为防止重复打包
  }

主要是增加了这四处

nuxt跨域

 

 index.vue

  mounted () {
    this.$axios.post('/admin/query/list')
      .then(res => {
        console.log(res)
      })
      .catch(e => {
        console.log(e)
      })
  }

 

相关文章:

  • 2021-12-06
  • 2021-07-18
  • 2021-05-17
  • 2022-01-05
  • 2021-08-11
  • 2021-11-27
  • 2021-04-09
猜你喜欢
  • 2021-07-11
  • 2021-11-06
  • 2021-12-26
相关资源
相似解决方案