Nuxt uses axios to request back-end data

npm install axios -s

Configure the public request in the pluginsnew axiosfile
...
Import from the vue page

  import axiosApi from "../plugins/axios";

In the asyncDatarequest rendering data

  export default {
    
    
    data() {
    
    
      return {
    
    
        info: []
      }
    },

    async asyncData() {
    
    
      const res = await axiosApi("getData", {
    
    }, "post")
      return {
    
    
        info: res.data.top_four
      }
    },
  }

asyncData method

The asyncData method is called every time the component is loaded.
asyncData can be called before the server or route update
. The data returned by asyncData is merged into the data method of the component. The
asyncData method is called before the component is initialized. The interior of the method cannot be thisused to reference the component. Instance object

View the source code to view the requested data
Insert picture description here

Guess you like

Origin blog.csdn.net/AK852369/article/details/111219860