Axios-module: Easier methods to retrieve axios instance

Created on 17 Aug 2017  路  10Comments  路  Source: nuxt-community/axios-module

I think this was brought up before this module was moved, but are there any plans for making the the axios instance a bit easier to use? More specifically, I thought I saw someone mention an idea about making store actions a bit easier to use, instead of passing down axios.

Ideally, I'd love to be able to just import the instance and use it even outside of Nuxt. I'm not that familiar with the latest plugin system, but is there a way to get the current Nuxt app and import the plugins somehow?

This question is available on Nuxt.js community (#c12)

Most helpful comment

Hi. Thanks for your interest. Actually $axios is almost available everywhere context (nuxt early ctx or real vue ctx) is accessable. The only remaining exception is inside store actions which currently the only way is passing it using params. Fix already made on vuex project, we are just waiting for next release :) Will update the docs when available.

All 10 comments

Not the expert, but I'm pretty sure store.$axios works now.

Hi. Thanks for your interest. Actually $axios is almost available everywhere context (nuxt early ctx or real vue ctx) is accessable. The only remaining exception is inside store actions which currently the only way is passing it using params. Fix already made on vuex project, we are just waiting for next release :) Will update the docs when available.

@pi0, I have doubt about:

My doubt :

The only remaining exception is inside store actions which currently the only way is passing it using params.

In the next release, will be possible Store get instance of axios or $axios without passing him like param?

For now, how I can get axios in the clean way code without param ? Because i have more than 650 actions calling axios and id like to use axios-module in my actual projects. And Im usgin mode: 'spa'.

Thanks in advanced

@rAugustos yes, I think they're waiting on an upstream fix from Vuex first.

There is an additional problem.
How can middleware access axios?

export default async function (context) {
console.log(this.$axios) _// Is this right?_
}

Please let me know about this issue.

@hyeokluv I haven't tried it, but I'm pretty sure it's in context...I'm not sure what this refers to there. Try context.$axios instead.

I use auth middleware.

The reason for using axios in middleware is that,

export default async function (context) {
聽聽 if (! Context.store.state.userProfile) {
聽聽聽聽 await context.store.dispatch ('redirectTo', {
聽聽聽聽聽聽 axios: context.$axios, // Not working ...
聽聽聽聽聽聽 path: context.route.path,
聽聽聽聽 }}
聽聽聽聽 return context.redirect (/ login)
聽聽 }
}

To use like.

Save the path you want to reach from a non-logged-in user in the session, and then route it after login.

However, the context. $ Axios is undefined.
Please let me know if there is a way to import axios or any other way to accomplish my purpose.

Finally is available with [email protected] 馃嵏 Will be shipped with nuxt rc8. For earlier testing, the only required thing is yarn add vuex to install latest version. Then you can directly use axios like this:

export default {
  actions: {
    getProfile() {
       const profile = await this.axios.$get('user/profile')
    }
  }
}

@pi0 Thank you so much!

@pi0 Thank you for your steady reply.

I'm already using nuxt version 1.0.0-alpha.3. I want to update nuxt, but I can not do it because of a conflict.

Is there a safe way to update nuxt?
Or is there no other way to use axios in middleware?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

megapctr picture megapctr  路  5Comments

seanwash picture seanwash  路  6Comments

monty086 picture monty086  路  3Comments

kaboume picture kaboume  路  4Comments

mrleblanc101 picture mrleblanc101  路  4Comments