java1234开源博客系统
博客信息

Ajax框架vue-resource&axios

0
发布时间:『 2019-07-24 22:29』  博客类别:Vue.js  阅读(3307) 评论(0)

vue的ajax框架有vue-resource和axios;

vue-resource插件非官方库,目前vue1,2都支持;

axios是ajax通用请求库,vue官方推荐;


我们先来看下vue-resource ;

主页:https://github.com/pagekit/vue-resource


安装下vue-resource

npm install vue-resource


全局引入,使用该插件;

main.js里

import VueResource from 'vue-resource'

// 声明使用插件 底层会给vm和组件对象添加一个属性 $http

Vue.use(VueResource)


App里我们测试一个接口;

let url='https://www.apiopen.top/novelSearchApi?name=盗墓'

this.$http.get(url).then((response)=>{  // 成功的回调

console.log('success')

console.log(response.body)

},(response)=>{  // 失败的回调

console.log('error')

console.log(response.text())

})


具体详细用法,请看文档:

https://github.com/pagekit/vue-resource/blob/develop/docs/http.md


我们再来看下axios用法,类似的;

主页:https://github.com/axios/axios


安装下axios;

npm install axios


axios使用是哪里用到哪里引入;

import axios from 'axios'


axios.get(url).then(response=>{

 console.log(response)

}).catch(error=>{

 alert('error')

 console.log(error)

})





关键字:   axios  

上一篇:es6 Promise 对象

下一篇:es6 Generator函数

关注Java1234微信公众号
博主信息
Java1234_小锋
(知识改变命运,技术改变世界)
Powered by Java1234 V3.0 Copyright © 2012-2016 Java知识分享网 版权所有