一、
1.首先npm i axios -s 安装
2.在main.js中导入axios
//引入axios
import axios from "axios"
//挂载在vue上面
Vue.prototype.$axios = axios
3.在created中使用:
this.$axios.get(this.url + '/pc/course/detail/'+this.$router.currentRoute.query.id).then(res=>{
// console.log(res.data.data)
this.coverFileUrl = res.data.data.coverFileUrl
})
二、
首先npm i axios -s 安装
在新建文件夹中创建两个js
import axios from 'axios'
// 使用自定义的配置文件发送请求
const instance = axios.create({
baseURL: '',
timeout: 5000,
headers: {
}
});
// 添加请求拦截器
instance.interceptors.request.use(function (config) {
// 在发送请求之前做些什么
return config;
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
});
// 添加响应拦截器
instance.interceptors.response.use(function (response) {
instance// 对响应数据做点什么
if(response.status === 200){
return response;
}else{
console.error("请求错误")
console.error(response)
}
return response;
}, function (error) {
// 对响应错误做点什么
return Promise.reject(error);
});
export default instance
c.在请求js文件中,如:
import http from './axios'
//获取热门新闻列表
function getHotNews(pageNum=1,pageSize=15){
let form=new URLSearchParams()
form.append('pageNum',pageNum)
form.append('pageSize',pageSize)
return http.post('/api/api/cms/article/open/hot/list',form)
}
d.在请求中组件中导入封装好的请求函数。如:
import {login} from "@/http/api";
methods{
login(this.username,this.password).then(res=>{
if(res.data.code==500){
//登陆失败通知
Notify('用户不存在/密码错误');
}else {
// 成功通知
Notify({ type: 'success', message: '登陆成功' });
this.$router.push('/mine')
// this.$router.go(-1)
this.$store.commit('change3',{userinfo:res.data})
}
})
}