简体   繁体   English

如何访问 axios 实例的 baseURL

[英]How to access baseURL of axios instance

I put this question up in order for easy access of the solution for fellow developers searching for an answer我提出这个问题是为了让寻找答案的其他开发人员轻松访问解决方案

//If you have an axios instance declared in a module similar to this, say, api.js //如果你有一个 axios 实例在类似这样的模块中声明,比如 api.js

var axios = require('axios');

var axiosInstance = axios.create({
    baseURL: 'https://example.com/foo/bar',
    /* other custom settings */
});

module.exports = axiosInstance;

//If you are accessing the module somewhere else, like this //如果你在其他地方访问模块,像这样

var api = require('./api');

api.get('relative/path')      //For a typical get call using an instance

But, you want to find the baseURL for some business requirement from an axios instance and not necessarily make a call, how do you find it?但是,您想从 axios 实例中找到某些业务需求的 baseURL,而不一定要拨打电话,您如何找到它?

https://github.com/axios/axios#global-axios-defaults https://github.com/axios/axios#global-axios-defaults

in your case should be : baseURL = api.defaults.baseURL在你的情况下应该是: baseURL = api.defaults.baseURL

My Api js file我的 Api js 文件

const instance = axios.create({ baseURL: "http://localhost:3157/api/v1", your base url headers: { "Content-type": "application/json", }, }); const instance = axios.create({ baseURL: "http://localhost:3157/api/v1", 您的基本 url 标头:{ "Content-type": "application/json", }, });

export default instance;导出默认实例;

import instance from 'api.js';从 'api.js' 导入实例;

let baseURL = instance.dafaults.baseURL;让 baseURL = instance.dafaults.baseURL;

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

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