Performance: Axios setup and configuration
This affects page load speed and interaction responsiveness by controlling how HTTP requests are made and managed in the app.
import axios from 'axios'; const apiClient = axios.create({ baseURL: 'https://api.example.com', timeout: 5000 }); export default { data() { return { data: null }; }, methods: { async fetchData() { const response = await apiClient.get('/data'); this.data = response.data; } } }
import axios from 'axios'; export default { data() { return { data: null }; }, methods: { fetchData() { axios.get('https://api.example.com/data') .then(response => { this.data = response.data; }); } } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct axios calls everywhere | Minimal | 0 | 0 | [!] OK |
| Single configured axios instance | Minimal | 0 | 0 | [OK] Good |
axios.create() in a Vue project?const api = axios.create({ baseURL: 'https://api.example.com', timeout: 1000 });
api.get('/users')
.then(response => console.log(response.data))
.catch(error => console.log('Error:', error.message));const api = axios.create({
baseURL: 'https://api.example.com',
timeout: '5000',
headers: { 'Content-Type': 'application/json' }
});Pass the test and this pattern moves unknown