当我通过带有URL的axios发送请求时axios连接api的url 与类星体开发服务器的URL,我怎么能忽略这一点 级联并仅在有任何配置的情况下发送API URL 带有类星体的axios的baseUrl?
src /启动 从“ axios”导入axios
export default async ({ Vue }) => {
axios.defaults.baseURL = 'http//:localhost:3000/'
Vue.prototype.$axios = axios
}
组件:
this.$axios({
url : 'backend/spots/all',
}).then(response=>{
this.allSlots = response.data
})
答案 0 :(得分:0)
我不确定,但是您可以尝试将axios.defaults.baseURL = 'http//:localhost:3000/'
更改为axios.defaults.baseURL = 'http://localhost:3000/'
(更改冒号)吗?
答案 1 :(得分:0)
根据类星体documentation,您可以尝试以下操作:
// src/boot/axios.js
const axiosInstance = axios.create({
baseURL: 'http//:localhost:3000'
})
export default async ({ Vue }) => {
Vue.prototype.$axios = axiosInstance
...
}
export { axiosInstance }
用于某些用途| js文件:
import { axiosInstance } from 'src/boot/axios'
axiosInstance.get('/some-endpoint')
答案 2 :(得分:0)
对于阅读本文的任何人。 根据类星体axios boot file documentation,您可以尝试更清洁的方法......
import Vue from 'vue'
import axios from 'axios'
// we add it to Vue prototype
// so we can reference it in Vue files as this.$axios
// without the need to import axios or use vue-axios
Vue.prototype.$axios = axios
// can also create an axios instance specifically for the backend API
const api = axios.create({ baseURL: 'https://api.example.com' })
Vue.prototype.$api = api
export { axios, api }