首页 > 移动平台 > 详细

axios

时间:2018-09-14 18:57:01      阅读:191      评论:0      收藏:0      [点我收藏+]

 

安装

npm install axios

main.js

//引入
import axios from ‘axios‘
//挂载到原型链
Vue.prototype.$http = axios

使用

// 获取模块
getModules(){
   this.$http.get(‘/warn/module‘ ).then((json) => {
      if (json.success == true) {
          this.modules = json.data;
       } else {
          this.$message(json.errorMsg);
       }
   }) 
}
//消除告警
 cancelAlarm(){
      this.$http.get(‘/warn/remove‘,{
      params:{
          reason:this.reason,
          id:this.alarmId
      }
       }).then((json) => {
          if (json.success == true) {
                  this.$message(‘消除成功!‘);
           } else {
                  this.$message(json.errorMsg);
            }
     }) 
}

并发请求。注意返回的是promise对象才可以喔

 //并发请求,并用spread方法拆分
getTest(){
  this.$http.all([this.getModules(),this.getTypes()]).then( 
  this.$http.spread((res1,res2)=>{
  console.log(res1)
  console.log(res2)
  }))
},

拦截器

axios.interceptors.request.use((config) => {
    //拦截之前做的事
    config.headers[‘X-Requested-With‘] = ‘XMLHttpRequest‘
    config.url = config.url;
    return config;
},(error)=>{
    //请求错误做的事
    return Promise.reject(error);
});

 

axios

原文:https://www.cnblogs.com/PeriHe/p/9648129.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!