国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vue利用axios來完成數據的交互

2019-11-19 14:06:59
字體:
來源:轉載
供稿:網友

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

效果圖:

參考網址:https://github.com/axios/axios

總結

以上所述是小編給大家介紹的vue利用axios來完成數據的交互,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 隆子县| 铜川市| 米泉市| 禄丰县| 中卫市| 海伦市| 横峰县| 光泽县| 贵州省| 汉源县| 陆良县| 临澧县| 仙游县| 娱乐| 桂东县| 中牟县| 康平县| 武清区| 邵东县| 浏阳市| 双峰县| 大渡口区| 泰宁县| 博乐市| 孟连| 行唐县| 滁州市| 盐亭县| 大名县| 禄劝| 井冈山市| 扬中市| 图片| 祁门县| 壶关县| 长沙县| 邵阳市| 井研县| 龙里县| 定西市| 大关县|