[Vuejs]-How to use env var for API in NuxtJS axios?

3👍

You’re using Nuxt’s Axios module in the project.
There’s no need to import Axios for using it. By importing it, you’re using a new instance, not the one you’ve actually injected with baseURL.

Just replace await axios.get with await this.$axios.get and remove your import.

<script>
export default {
  name: 'App',
  data() {
    return {
      categories: [],
      error: null,
    }
  },
  async mounted() {
    try {
      const response = await this.$axios.get('categories')
      this.categories = response.data
    } catch (error) {
      this.error = error
    }
  },
}
</script>

Leave a comment