Axios请求数据的使用方法浅析
2019-02-28 17:05
661 查看
请求数据的第三方插件Axios,可以支持vue,node ,react
axios:基于Promise的HTTP客户端,用于浏览器和node.js
使用步骤
[code]1:安装axios npm i axios --save / cnpm 2: 哪里用就在那里引入 import Axios from 'axios' / const axios = require('axios'); 3:直接在组件中应用 axios.get('/user') .then(function (response) { // handle success console.log(response); }) .catch(function (error) { // handle error console.log(error); }) .then(function () { // always executed });
代码示例:
请求数据前
[code]<template> <div id="life"> <h3>生命首期函数</h3> <ul> <li v-for="(item,index) in list" :key="index"> {{item.catname}} </li> </ul> <br> <button @click="getData()">请求数据</button> </div> </template> <script> import Axios from 'axios' export default { data(){ return{ list:[] } }, methods: { getData(){ var api='http://www.phonegap100.com/appapi.php?a=getPortalCate' Axios.get(api) .then((response)=> { //尽量用箭头函数,避免this的指向问题 this.list=response.data.result }) .catch((error)=> { // handle error console.log(error); }) } }, mounted(){ /* 渲染dom完成时请求数据,操作Dom时常用 */ console.log('实力挂载完成'); this.getData(); } }
请求数据后:
关于axios请求数据的具体步骤就分析到这,欢迎各路大神指教更好 的套路!!!
相关文章推荐
- vue axios数据请求及vue中使用axios的方法
- 使用load()方法异步请求数据
- 使用AFNetworking 2.0 请求数据时出现错误 Request failed: unacceptable content-type: XXXX 解决方法
- HTTP客户端之使用request方法向其他网站请求数据
- 使用load()方法异步请求数据
- Python中使用socket发送HTTP请求数据接收不完整问题解决方法
- 使用request方法向其他网站请求数据
- 在外部方法使用Ajax请求返回的数据
- Python使用scrapy采集数据时为每个请求随机分配user-agent的方法
- AFNetworking框架IOS网络数据请求由浅入深的使用方法
- Xutils请求数据,加载图片基本使用方法
- vue 使用 axios 发 post 请求,后台无法接收到数据
- Postman 使用post方法请求数据
- mock.js在vue中的使用(axios请求数据)
- 使用sql server management studio 2008 无法查看数据库,提示 无法为该请求检索数据 错误916解决方法
- kevinsawicki 的 HttpRequest使用Post方法请求数据
- IOS中网络数据请求过程详解(GET POST方法使用)
- NSString类方法同步请求、使用NSData方法同步请求 下载图片、数据
- iOS开发中大部分App的网络数据交换是基于HTTP协议的。本文将简单介绍在Swift中使用HTTP进行网络请求的几种方法。
- 在 Vue 结合 Axios 使用过程 中 post 方法,后台无法接受到数据问题