vue中axiso全局使用

1 篇文章 0 订阅

在vue中如果要使用axiso来请求数据,
直接在main,js中

import axios from 'axios'
Vue.prototype.$axios = axios;

举个例子
在这里插入图片描述

<template>
  <div>
      <h1 class='res'>asixo请求{{page}}</h1>
      <div class="box">
         <ul>
            <li v-for="(item,index) in arrList" :key="index">
               {{item.id}} <p>{{item.title}}</p>
            </li>
         </ul>
         <div style="geight:30px;margin-top:30px">
               <span class="p" v-on:click="pageBack">上一页</span>
               <span class="p1" v-on:click="pageNext">下一页</span>
         </div>        
      </div> 
  </div>
</template>
<script>
export default {
  name: 'Hostory',
  data () {
    return {
      msg: 'Axiso请求',
      page:1,
      arrList:[],
    }
  },
  methods: {
      list:function(){
         var self = this;
         this.$axios.get('http://web.source.91yunshi.com/content?type=3',{
            params: {
              pageNo: this.page,
              pageSize:6
            }
          })
          .then(function (response) {
            self.arrList=response.data.data
          })
          .catch(function (error) {
            console.log(error);
          });
       },
        //下一页
        pageNext:function(){
            this.page++;
            this.list();
        },
        //上一页
      pageBack:function(){

        if(this.page==1){

          this.list();
            return false;
        }else{
            this.page--;
             this.list();
        }
        },

    },
  created:function(){
      this.list()
  },
}
</script>

<style scoped>
body{
  font-family: '微软雅黑', 
}
.res{
  color:red;
  text-align: center;
}
ul li{
     cursor: pointer;
}
.box{
  width: 400px;
  height: 600px;
  margin: 0 auto;
  border: 1px solid #ccc;
  overflow: hidden;
}
.p1{
  float: right;
  margin-top: -20px;
   cursor: pointer;
   border: 1px solid red;
   margin-right: 20px;
}
.p{
   cursor: pointer;
   border: 1px solid red;
   margin-left: 20px;
   float: left;
}
</style>

老习惯,如果你看到了对你有帮助,麻烦点个赞,

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Vue是一种流行的JavaScript框架,用于开发Web应用程序。Axios是一种用于发送异步请求的JavaScript库,可轻松处理HTTP请求和响应。封装axios并将其应用到Vue全局,可以让我们在任何Vue组件调用API时更加方便和简单。 首先,我们需要在Vue应用的main.js文件引入axios库: ``` import vue from 'vue'; import axios from 'axios'; ``` 这将使我们可以在整个Vue应用使用axios。然后,我们将向Vue原型添加axios实例,并在全局进行注册: ``` vue.prototype.$axios = axios; ``` 这使我们可以在每个Vue组件使用`this.$axios`,从而发送HTTP请求。但是,我们可能还需要在请求之前和之后进行一些操作,例如设置请求头、拦截错误等。因此,我们可以创建一个新的axios实例并对其进行自定义配置,以便在应用程序进行全局使用。 ``` const axiosInstance = axios.create({ baseURL: "http://example.com/api", headers: { "Content-Type": "application/json" } }) axiosInstance.interceptors.response.use( response => response.data, error => { console.log(error); return Promise.reject(error); } ); vue.prototype.$axios = axiosInstance; ``` 这样,我们可以使用`this.$axios`来发出修改后的HTTP请求,并在发送请求之前和之后执行我们的操作。我们还可以使用拦截器对响应进行预处理,以便在需要时对响应进行统一处理。最后,封装Axios的方法可以根据项目需求进行自定义配置,以提高效率并使代码更加可读。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1登峰造极

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值