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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

1登峰造极

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

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

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

打赏作者

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

抵扣说明:

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

余额充值