目录结构:
api.js
export default { myTopic: '/api/subscribe-data/post/cat' }
request.js
import axios from 'axios' import apis from './api' import root from './root' const request = axios.create({ baseURL: root.httpUrl }) export default { // post post (urlKey, params) { return request({ headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'}, method: 'post', url: apis[urlKey], data: params }) } }
root.js
const root = { httpUrl: '//neican.ink:2080' } export default root
在main.js中
import request from '../apis/request.js'
Vue.prototype.$request = request
在组件中就可以使用request.post进行请求了
this.$request.post('myTopic').then((res)=>{
console.log(res)
})