vue 学习笔记—Resource

时间:2023-03-09 03:01:54
vue 学习笔记—Resource

1.首先是引入 vue 学习笔记—Resource

或者用npm来安装  cnpm i vue-resource --save(推荐)

3.提供的api

vue 学习笔记—Resource

vue 学习笔记—Resource

关于请求写法:

get(){
// get请求
this.$http.get( // get方法里面('地址',{params:{},headers{}); 由于是get方法所以参数都要放到params里面 然后整个参数被对象包括
'package.json',
{
params:{
userId:'101'
},
headers:{
token:'abcd'
}
}
).then(
res=>{
this.msg = res.data;
},
error=>{
this.msg = error;
}
);
},
post(){
// post请求
this.$http.post( //post方法里面 post('地址',{userId:''},{headers:{}}) post方法参数直接在一个对象里面写 然后请求头也是单独一个函数里面写
'package.json',
{
userId:'102'
},
{
headers:{
access_token:'abscd'
}
}
).then(
res=>{
this.msg = res.data
}
)
}
//jsonp 请求 实际是<script>ajax传输 能解决跨域
jsonp(){
 this.$http.jsonp('http://www.imooc.com/course/AjaxCourseMembers?ids=866').then(res=>{
  this.msg = res.data;
  });
}
//在mounted里面可以写全局拦截
// 全局拦截 全部的请求
Vue.http.interceptors.push(function(res,next){
  console.log('请求前');   next(respon=>{
      console.log('请求后');
return respon;
});
});
// methosd同级方法 http{} 能统一定义请求路径
http:{
root:'http://localhost:63342/vueactual/ops/156'
}
http 方式 类似jq ajax
ajax(){
this.$http({
url:'package.json',
params:{
userId:'123'
},
headers:{
token:'123',
tokens:'123'
},
timeout:50,
before:function(){
console.log('before init')
} }).then(res=>{
this.msg = res.data;
});
}