1.封装axios
1.新建js文件
2.添加代码
import axios from 'axios'
let base = ''
function request (url, params, type) {
if (type == null || type === ' ') {
type = 'post'
}
return axios({
method: type,
url: `${base}${url}`,
data: params,
headers: {
'Content-Type': 'application/json'
}
})
}
export default {
request
}
3.引入
import req from '../../utils/requestUtils'
2.跨域
文件位置在 config/index.js
源代码不记得长什么样子了,改成如下:
target是要写上你要跳转的后端地址
/api 是在调用的时候要加上例如
嫌麻烦也可以不写,直接用 / 就行
添加了完成配置之后,一定要重启前端项目才会生效。