vue利用axios来完成数据的交互实例及安装方法

csdn原文连接https://blog.csdn.net/lily2016n/article/details/79654957css

axios基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用 如今Vue官方推荐的网络通讯库再也不是vue-resource了,推荐使用axios。因此学习了下,总结以下。vue

1、功能特性 一、在浏览器中发送 XMLHttpRequests 请求 二、在 node.js 中发送 http请求 三、支持 Promise API 四、拦截请求和响应 五、转换请求和响应数据 六、自动转换 JSON 数据 七、客户端支持保护安全免受 XSRF 攻击 2、axios的安装方法(官方给了3种方法) 一、npm安装node

$ npm install axios
复制代码

二、bower安装ios

$ bower install axios
复制代码

三、直接使用cdngit

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
复制代码

3、安装步骤 这里我使用npm的方法步骤: ①首先在npm中输入npm install axios ②在main.js加上配置 import axios from 'axios' Vue.prototype.$http = axios github

这里写图片描述

4、请求实例 点击获取数据能够取到想要的数据npm

<template>
  <div class="tabbar">
    <p>首页</p>
    <button v-on:click = 'goback'>获取数据</button>
    <div class="new_wrap" v-for="items in item">
      <div class="newcard">
        <div>
           <p>{{items.issuer_nickname}}.</p>
        </div>
        <div>
           {{items.title}}
        </div>
        <div class="pic">
           <img :src="items.cover">
        </div>  
      </div>

      <br>
     </div>
  </div>
</template>
<script>
export default {
  name: 'tabbar',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App',
      item: []
    }
  },
  methods:{
    goback:function(){
      console.log('hah');
      this.$http.get('url') //把url地址换成你的接口地址便可
        .then(res => {
          //this.request.response = res.data
          this.item = res.data.data.item; //把取item的数据赋给 item: []中
          console.log(res.data.data.item);
          if (res.data.code == '0') {
            console.log('haha');
          }else{
            alert('数据不存在');
          }
        })
        .catch(err => {
          alert('请求失败');
        })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
*{margin: 0;padding: 0;}
@function torem($px){//$px为须要转换的字号
    @return $px / 100px * 1rem; //100px为根字体大小
}
ul{
  width: 100%;
  position: absolute;
  bottom: 0;
  li{
    width: torem(187.5px);
    float:left;
    height: torem(98px);
    text-align:center;
    background: #ccc;
    }

}
img{
      width: torem(200px);
      height: torem(200px);
    }

</style>

复制代码

效果图: axios

这里写图片描述
参考网址: github.com/axios/axios segmentfault.com/a/119000000…
相关文章
相关标签/搜索