您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

vue+axios实现数据交互

2026/2/7 15:11:06发布11次查看
这次给大家带来vue+axios实现数据交互,vue+axios实现数据交互的注意事项有哪些,下面就是实战案例,一起来看一下。
axios基于 promise 的 http 请求客户端,可同时在浏览器和 node.js 中使用
现在vue官方推荐的网络通信库不再是vue-resource了,推荐使用axios。所以学习了下,总结如下。
一、功能特性
1、在浏览器中发送 xmlhttprequests 请求
2、在 node.js 中发送 http请求
3、支持 promise api
4、拦截请求和响应
5、转换请求和响应数据
6、自动转换 json 数据
7、客户端支持保护安全免受 xsrf 攻击
二、axios的安装方法(官方给了3种方法)
1、npm安装
$ npm install axios
2、bower安装
$ bower install axios
3、直接使用cdn
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
三、安装步骤
这里我使用npm的方法步骤:
①首先在npm中输入npm install axios
②在main.js加上配置
import axios from ‘axios'  vue.prototype.$http = axios
四、请求实例
点击获取数据可以取到想要的数据
<template>  <p class="tabbar">   <p>首页</p>   <button v-on:click = 'goback'>获取数据</button>   <p class="new_wrap" v-for="items in item">    <p class="newcard">     <p>       <p>{{items.issuer_nickname}}.</p>     </p>     <p>       {{items.title}}     </p>     <p class="pic">       <img :src="items.cover">     </p>     </p>    <br>    </p>  </p> </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>
效果图:
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
js怎么储存原始值与引用值
js用正则判断出生日期
以上就是vue+axios实现数据交互的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product