1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > vue使用ajax库 Vue 中使用Ajax请求

vue使用ajax库 Vue 中使用Ajax请求

时间:2019-02-02 17:05:37

相关推荐

vue使用ajax库 Vue 中使用Ajax请求

Vue 项目中常用的 2 个 ajax 库

(一)vue-resource

vue 插件, 非官方库,vue1.x 使用广泛

vue-resource 的使用

在线文档 /pagekit/vue-resource/blob/develop/docs/http.md

下载

npm install vue-resource--save

编码

// 引入模块 ,注意应该在App.vue中引入和声明

import VueResource from 'vue-resource'

// 声明使用

Vue.use(VueResource)

// 通过 vue组件对象发送 ajax 请求

this.$http.get('/someUrl').then((response)=>{

//successcallback

console.log(response.data)//返回结果数据

},(response)=>{

//errorcallback

console.log(response.statusText)//错误信息

})

(二)axios

通用的 ajax 请求库, 官方推荐,vue2.x 使用广泛

axios 的使用

在线文档 /pagekit/vue-resource/blob/develop/docs/http.md

下载

npm install axios--save

编码

// 引入模块

import axios from 'axios'

// 发送 ajax 请求

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

//successcallback

console.log(response.data)// 得到返回结果数据

}) .catch(error=>{

console.log(error.message)//错误信息

})

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。