Using Defaults with Axios
While the code in Exercise 10.01, Using Axios to Load Data from an API works well, let's consider a slightly more advanced example. One of the features of Axios
is the ability to set up defaults that are used in future calls. If you look at the two calls made in the preceding code, you can see they are similar. You can update the created
method to make use of this:
created() { Â Â const api = axios.create({ Â Â Â Â baseURL:'https://swapi.dev/api/', Â Â Â Â transformResponse(data) { Â Â Â Â Â Â data = JSON.parse(data); Â Â Â Â Â Â return data.results; Â Â Â Â } Â Â }); Â Â api.get('films') Â Â .then(res => this.films = res.data); Â Â api.get('starships') Â Â .then(res => this.ships = res.data); }
In this updated version, we switch to an instance of Axios
. A default baseURL...