最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

vue可以用ajax,Vue 中使用Ajax请求

互联网 admin 3浏览 0评论

vue可以用ajax,Vue 中使用Ajax请求

Vue 项目中常用的 2 个 ajax 库

(一)vue-resource

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

vue-resource 的使用

下载

npm install vue-resource --save

编码

// 引入模块 ,注意应该在入口文件main.js中引入和声明

import VueResource from 'vue-resource'

// 声明使用

Vue.use(VueResource)  //内部会给vm对象和组件对象添加一个属性:$http

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

this.$http.get('/someUrl?foo=bar').then(

response=>{//successcallback

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

},

response=>{//errorcallback

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

})

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

this.$http.post('/someUrl', {foo: 'bar'}).then(

response=>{//successcallback

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

},

response=>{//errorcallback

console.log(response.statusTe

vue可以用ajax,Vue 中使用Ajax请求

Vue 项目中常用的 2 个 ajax 库

(一)vue-resource

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

vue-resource 的使用

下载

npm install vue-resource --save

编码

// 引入模块 ,注意应该在入口文件main.js中引入和声明

import VueResource from 'vue-resource'

// 声明使用

Vue.use(VueResource)  //内部会给vm对象和组件对象添加一个属性:$http

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

this.$http.get('/someUrl?foo=bar').then(

response=>{//successcallback

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

},

response=>{//errorcallback

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

})

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

this.$http.post('/someUrl', {foo: 'bar'}).then(

response=>{//successcallback

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

},

response=>{//errorcallback

console.log(response.statusTe

发布评论

评论列表 (0)

  1. 暂无评论