简体   繁体   English

如何在 js 文件中导入 Nuxt 模块

[英]How to import Nuxt modules inside a js file

What I'm trying to do is to import Nuxt modules inside a js file.我想要做的是在 js 文件中导入 Nuxt 模块。 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.例如,我想在 js 文件中使用“@nuxt/axios”并从中创建一个实例,我不想从上下文中调用它 object 我想让该实例与任何 Nuxt 生命周期分开,并使用nuxt axios 模块不是正常的 axios package。

import axios from "@nuxt/axios"

const axiosInstace = axios.create();

export default axiosInstace;

Don't use these modules directly.不要直接使用这些模块。 @nuxt/axios is just a wrapper for the axios module. @nuxt/axios只是axios模块的包装器。 Just use that instead ( NPM ).只需使用它( NPM )。 Same goes for most other nuxt modules.大多数其他 nuxt 模块也是如此。

Just like what @Florian Pallas mentioned.就像@Florian Pallas 提到的那样。 Don't use these modules.不要使用这些模块。 use NPM or/and extend Axios from plugins使用 NPM 或/和从插件扩展 Axios

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM