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...