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)
        })
    },

最后進入這個頁面,在控制台看到打印即表示請求成功了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM