sparkyspider
sparkyspider

Reputation: 13509

Nuxt: how do I access axios within the fetch() method?

I'm using nuxt and am looking for a way to access the axios object from within the nuxt fetch() method. Unfortunately I can't seem to access it. This is what I've tried...

    async fetch({store, params}) {
        // const result = await axios.$get('/api/v2/inventory/3906?apiKey=xxx');
        // const result = await this.axios.$get('/api/v2/inventory/3906?apiKey=xxx');
        // const result = await $axios.$get('/api/v2/inventory/3906?apiKey=xxx');
        store.commit('property/setProperty', result);
    }

this is (obviously) not available and $axios and axios are undefined.

I do have @nuxtjs/axios defined in my nuxt.config.js

modules: [
    'nuxt-buefy',
    '@nuxtjs/style-resources',
    '@nuxtjs/device',
    '@nuxtjs/axios',
    '@nuxtjs/proxy'
],

Upvotes: 2

Views: 2863

Answers (1)

sparkyspider
sparkyspider

Reputation: 13509

@balexandre should actually get the credit for this. Thanks for the help.

This is an incredibly stupid question I asked, but I'm going to post the answer here in case anyone else has the same experience brain-fart.

import axios from 'axios' // don't forget me!

... later...

fetch() {
  async fetch({store, params}) {
    const result = await axios.get('/api/v2/inventory/3906?apiKey=xxx');
    store.commit('property/setProperty', result);
  }
}

Upvotes: 1

Related Questions