0

What I'm trying to do is to import Nuxt modules inside a js file. for example I want to use "@nuxt/axios" inside a js file and create an instance from it and I don't want to call it from context object I want to make that instance separate from any Nuxt life cycle and also use the nuxt axios modules not the normal axios package.

import axios from "@nuxt/axios"

const axiosInstace = axios.create();

export default axiosInstace;

2 Answers 2

1

Don't use these modules directly. @nuxt/axios is just a wrapper for the axios module. Just use that instead (NPM). Same goes for most other nuxt modules.

Sign up to request clarification or add additional context in comments.

1 Comment

Look what I'm trying to create is to make the baseURL in axios to be the same as the domain with some prefixes or in other words to more dynamic, and that happens if I have the context object, I need I way to create the same functionally without the context object
1

Just like what @Florian Pallas mentioned. Don't use these modules. use NPM or/and extend Axios from plugins

Edited answer: I remember seeing this code somewhere

const myAPI = axios.create({
  baseURL: 'https://api.domaine.com/',
  withCredentials: false,
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json'
  },
  params: {
    order: 'desc',
    sort: 'name'
  }
})

function request(id) {
    myAPI.get('/users/'+id);
}

request(123)

5 Comments

Look what I'm trying to create is to make the baseURL in axios to be the same as the domain with some prefixes or in other words to more dynamic, and that happens if I have the context object, I need I way to create the same functionally without the context object
check my answer
in my case, the baseURL is depending on the domain because I try to create a saas project so I cant set the baseURL directly, I want to prefix it. I tried to create that functionality in "@nuxt/axios" because "@nuxt/axios" has a function "onRequest" this function like "axios interceptors.request" on this function there is common object and inside of it there is the sented domain, but this only happens in "@nuxt/axios" not in normal "axios", that why I need to import this package to use it in different files without context object, or in normal axios prefix the domain in some way to be dynamic.
Have you tried this package? github.com/srph/axios-base-url
I think this package will help me because If you're using axios >=v0.8, please use the base url configuration provided by the library out of the box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.