Axios with Vue
Axios provides instances, interceptors, and convenient JSON handling. This lesson covers practical patterns, syntax, and mistakes to avoid.
Using Axios
Axios provides instances, interceptors, and convenient JSON handling.
Create one api instance and import it from composables/stores.
import axios from 'axios'
const api = axios.create({ baseURL: import.meta.env.VITE_API_URL })
export default api
Shared Axios instance.
Interceptors
Attach auth headers; normalize errors.
- Handle loading and errors.
- Keep composables tested.
- Prefer TypeScript when you can.
- Measure before optimizing.
Axios with Vue Cheatsheet
Quick reference for patterns covered in this lesson.
| Item | Example | Purpose |
| create | instance | Config |
| interceptors | cross-cutting | Auth |
| timeout | option | Safety |
| pinia | actions | Calls |
| TS | generics | Types |
| upload | FormData | Files |
Auth Refresh
Be careful with interceptor loops when refreshing tokens.
Common Mistakes
- Many ad-hoc axios calls with duplicated config.
- Logging tokens.
Key Takeaways
- Axios with Vue shows up often in Vue apps.
- Practice with a demo.
- Prefer clear APIs.
- Read official docs for edge cases.
Pro Tip
Keep token attachment in one request interceptor.