L
L
lodas2020-05-24 16:28:01
Vue.js
lodas, 2020-05-24 16:28:01

Nuxt why axios not working?

Hello! I decided to add the Axios Module to nuxt. I did everything according to the instructions:
1) Installed npm install @nuxtjs/axios
2) Added a module to nuxt.config.js

modules: [
    '@nuxtjs/axios',
  ],

3) copied the example for asyncData from the documentation with the right url
async asyncData({ $axios }) {
      const ip = await $axios.$get('https://jsonplaceholder.typicode.com/todos/1')
      return { ip }
    },

Gives an error message:
ERROR  Cannot read property '$get' of undefined                      16:18:09

  at asyncData (index.vue:49:0)
  at promisify (.nuxt/utils.js:262:0)
  at server.js:1455:82
  at Array.map (<anonymous>)
  at module.exports../.nuxt/server.js.__webpack_exports__.default (.nuxt/server.
js:222:0)


Please tell me how to fix this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Glebov, 2020-05-24
@lodas

asyncData accepts a context, see what is in it and look for axios

asyncData(ctx) {
  console.log(ctx)
}

This is how it should work
async asyncData({app}) {
      const ip = await app.$axios.$get('https://jsonplaceholder.typicode.com/todos/1')
      return { ip }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question