NUXT中使用自带axios

在nuxt.config.js中作如下配置

modules: [
    // Doc: https://github.com/nuxt-community/axios-module#usage
    '@nuxtjs/axios'
  ],
  /*
  ** Axios module configuration
  */
  axios: {
    proxy: true,
    prefix: '/api/',
    credentials: true
    // See https://github.com/nuxt-community/axios-module#options
  },

  proxy: {
    '/api/': { 
      target: 'https://h5api.zhefengle.cn',//这个网站是开源的可以请求到数据的
      pathRewrite: {
         '^/api/': '/',
         changeOrigin: true
      }    
    }
  },

使用的时候如下(一定要加this关键字,axios前面的$不能丢)

mounted() {
        this.$axios.get("/index.html").then(res=>{
            console.log(res)
        })
    },
原文地址:https://www.cnblogs.com/yddzyy/p/13269887.html