最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access baseURL of axios instance - Stack Overflow

programmeradmin3浏览0评论

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

var axios = require('axios');

var axiosInstance = axios.create({
    baseURL: '',
    /* 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?

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

var axios = require('axios');

var axiosInstance = axios.create({
    baseURL: 'https://example./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?

Share Improve this question asked Jul 1, 2021 at 12:37 Susmita SilSusmita Sil 2132 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

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

in your case should be : baseURL = api.defaults.baseURL

My Api js file

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

export default instance;

import instance from 'api.js';

let baseURL = instance.dafaults.baseURL;




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

//Use this --> "axiosInstance.getUri()"
console.log(axiosInstance.getUri());
<script src="https://cdn.jsdelivr/npm/axios/dist/axios.min.js"></script>
发布评论

评论列表(0)

  1. 暂无评论