Mustafa El-Gaml
Mustafa El-Gaml

Reputation: 93

How to import Nuxt modules inside a js file

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;

Upvotes: 0

Views: 1209

Answers (2)

AlmoDev
AlmoDev

Reputation: 969

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)

Upvotes: 1

Florian Pallas
Florian Pallas

Reputation: 582

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.

Upvotes: 1

Related Questions