Skip to content

Using axios? #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SomeoneIsWorking opened this issue Jun 5, 2020 · 9 comments · Fixed by #47
Closed

Using axios? #39

SomeoneIsWorking opened this issue Jun 5, 2020 · 9 comments · Fixed by #47
Labels
question Further information is requested

Comments

@SomeoneIsWorking
Copy link

SomeoneIsWorking commented Jun 5, 2020

There is a plugin that registers an axios instance but

  • Pages using axios use it with static access import axios from "axios"
  • I can't figure out how to reference it inside a Vue component: this.axios and Vue.axios show compile errors
@liborpansky
Copy link
Contributor

Hi, you have to create typings file for axios plugin, for example: ClientApp/src/plugins/axios.d.ts

import { AxiosStatic } from "axios";

declare module "vue/types/vue" {
  interface Vue {
    axios: AxiosStatic;
  }

  interface VueConstructor {
    axios: AxiosStatic;
  }
}

Then Vue components can access registered plugin instance this.axios...

@liborpansky liborpansky added the question Further information is requested label Jun 5, 2020
@SomeoneIsWorking
Copy link
Author

SomeoneIsWorking commented Jun 5, 2020

Thank you. Should the Forecast page get updated then or?

@liborpansky
Copy link
Contributor

I will update it later, in this case I will try to update also plugin registration from js to ts file and create typings declaration for the registration function.

@ddiiorio
Copy link

ddiiorio commented Jul 9, 2020

Also interested in how to use the axios plugin properly. I can import it in a Vue component that uses javascript with import axios from '../../plugins/axios' and it will work, but I don't believe that's the proper way to do it.

I also tried creating the axios.d.ts file above and then use axios in a typescript Vue component with this.axios and it will work, but see a warning in VS Code Property 'axios' does not exist on type 'component'.Vetur(2339)

@glebov21
Copy link
Contributor

glebov21 commented Sep 18, 2020

UP.
We have ClientApp/src/plugins/axios.js already, but how can i use them in my components?
FetchData example use default axios object instead of prepared object in ClientApp/src/plugins/axios.js

window.axios working, but this.$axios is better (not working)

@ddiiorio
Copy link

ddiiorio commented Sep 21, 2020

@liborpansky what about using axios within the vuex store? Currently in the code there is the import axios from "axios" import statement in actions.ts, so it wouldn't be using the plugin instance (correct me if I'm wrong)

Using this.$axios produces the error "Property '$axios' does not exist on type 'Store'.ts(2339)"

@liborpansky
Copy link
Contributor

You're right, the plugin instance is not accessible in the vuex store. You can import direct axios instance in store, but in this way you would miss any logic you applied in request / response interceptors...

There's a dirty workaround to pass your Vue instance as an action parameter, so you can access any plugin registered, but it's not an elegant solution I would choose.

Generally I end up with a sort of API Service class responsible for API calls, which uses directly axios non plugin instance that is being imported to all store modules and components directly to encapsulate http calls.

@ddiiorio
Copy link

Thanks I will look into that

@glebov21
Copy link
Contributor

I think, what using axios in "store" layer is not better idea for architecture. Maybe i'm wrong :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants