nuxtJs中直接使用自带的@nuxtjs/axios

最初我以为在nuxtjs中是需要重新npm install axios,但是其实nuxtjs自己集成了这个数据渲染方法

你只需在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
      }    
    }
  },

然后在你的页面可以直接请求数据了

mounted() {
        this.$axios.get("/index.html").then(res=>{
            console.log(res)
        })
    },

最后进入这个页面,在控制台看到打印即表示请求成功了

原文地址:https://www.cnblogs.com/ldlx-mars/p/9959226.html