vue-resource:

推荐教程:https://www.runoob.com/vue2/vuejs-ajax.html

1. 需要安装vue-resource模块, 注意加上 --save

npm install vue-resource --save / cnpm install vue-resource --save

2. main.js引入 vue-resource

import VueResource from 'vue-resource';

3. main.js

Vue.use(VueResource);

4. 在组件里面直接使用

this.$http.get(地址).then(function(res){

},function(err){

})

实例:

<template>
<div>
<h3>home组件</h3>
<button @click="addList()">加载</button>
<ul>
<li v-for="item in list">{{item.title}}</li>
</ul>
</div>
</template> <script>
export default {
name: "home",
data(){
return{
list:[]
}
},
methods:{
addList(){
//请求数据
var api='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1';
this.$http.get(api).then((res)=>{
this.list = res.body.result;
},(err)=>{
console.log(err)
})
}
},
mounted() { //请求数据,操作dom可在这进行
console.log('模板编译完成4')
this.addList();
},
beforeCreate() {
console.log('实例刚刚被创建1')
},
created() {
console.log('实例已经创建完成2')
},
beforeMount() {
console.log('模板编译之前3')
}, beforeUpdate() {
console.log('数据更新之前')
},
updated() {
console.log('数据更新完毕')
},
beforeDestroy() {//页面销毁前报错数据
console.log('实例销毁之前')
},
destroyed() {
console.log('实例销毁完成')
}
}
</script> <style scoped> </style>
vue-resource 提供了 7 种请求 API(REST 风格):
get(url, [options])
head(url, [options])
delete(url, [options])
jsonp(url, [options])
post(url, [body], [options])
put(url, [body], [options])
patch(url, [body], [options])

axios:

1.安装

cnpm install axios --save

2.哪里使用那里引入

<template>
<div>
<h3>home组件</h3>
<button @click="addList()">加载</button>
<ul>
<li v-for="item in list">{{item.title}}</li>
</ul>
</div>
</template> <script>
import axios from 'axios'
export default {
name: "home",
data(){
return{
list:[]
}
},
methods:{
addList(){
//请求数据
var api='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1';
axios.get(api).then((res)=>{
this.list = res.data.result;
}).catch((err)=>{
console.log(err)
})
}
},
mounted() { //请求数据,操作dom可在这进行
console.log('模板编译完成4')
this.addList();
},
}
</script> <style scoped> </style>

fetch:

https://github.com/camsong/fetch-jsonp

1.安装

cnpm install fetch-jsonp --save
 addList(){
var $that = this
//请求数据
// //注意: 第一个.then 中获取到的不是最终数据,而是一个中间的数据流对象;
// 注意: 第一个 .then 中获取到的数据, 是一个 Response 类型对象;
// 注意: 第二个 .then 中,获取到的才是真正的 数据;
var api='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1';
fetchJsonp(api,{
method:'get'
}).then(function (res) {
res.json().then((json)=>{
console.log(json)
$that.list = json.result;
})
}).catch(function (err) {
console.log('err',err)
})
}

vue请求数据的更多相关文章

  1. vue 使用 jsonp 请求数据

    vue 使用 jsonp 请求数据 vue请求数据的时候,会遇到跨域问题,服务器为了保证信息的安全,对跨域请求进行拦截,因此,为了解决vue跨域请求问题,需要使用jsonp. 安装jsonp npm ...

  2. vue 对象提供的属性功能、通过axio请求数据(2)

    1 Vue对象提供的属性功能 1.1 过滤器 过滤器,就是vue允许开发者自定义的文本格式化函数,可以使用在两个地方:输出内容和操作数据中. 1.1.1 使用Vue.filter()进行全局定义(全局 ...

  3. vue axios数据请求get、post方法的使用

    我们常用的有get方法以及post方法,下面简单的介绍一下这两种请求方法 vue中使用axios方法我们先安装axios这个方法 npm install --save axios 安装之后采用按需引入 ...

  4. 前端向服务器请求数据并渲染的方式(ajax/jQuery/axios/vue)

    原理: jQuery的ajax请求:complete函数一般无论服务器有无数据返回都会显示(成功或者失败都显示数据): return result

  5. $Django 前后端之 跨域问题(同源策略) vue项目(axios跨域请求数据)

    1 跨域问题(多个域之间的数据访问) #同源策略(ip port 协议全部相同) #本站的只能请求本站域名的数据 #CORS实现(跨域资源共享) #实现CORS通信的关键是服务器.只要服务器实现了CO ...

  6. vue使用element Transfer 穿梭框实现ajax请求数据和自定义查询

    vue使用element Transfer 穿梭框实现ajax请求数据和自定义查询 基于element Transfer http://element-cn.eleme.io/#/zh-CN/comp ...

  7. Vue之单文件组件的数据传递,axios请求数据及路由router

    1.传递数据 例如,我们希望把父组件的数据传递给子组件. 可以通过props属性来进行传递. 传递数据三个步骤: 步骤1:在父组件中,调用子组件的组名处,使用属性值的方式往下传递数据 <Menu ...

  8. vue vue-resource 请求数据

    main.js import Vue from 'vue'; import App from './App.vue'; /*使用vue-resource请求数据的步骤 1.需要安装vue-resour ...

  9. vue 请求后台数据2(copy)

    https://blog.csdn.net/vergilgeekopen/article/details/68954940 需要引用vue-resource 安装请参考https://github.c ...

随机推荐

  1. vivo 部分链表反转

    方法一:使用栈交换需要反转的数字 #include <iostream> #include <stack> #include "list.h" using ...

  2. [VBA]批量新建指定名称的工作表

    sub 批量新建指定名称的工作表() Dim i As Integer For i = 2 To 10    '根据实际情况修改i大小 Worksheets.Add after:=Worksheets ...

  3. Spring+hibernate 配置实例

    转自:http://www.cnblogs.com/hongten/archive/2012/03/10/java_spring_hibernate.html 项目结构: http://www.cnb ...

  4. java8 stream编程

    说明:这是基于同事的培训材料做的练习,记录下来,以作日后自己coding分析和改进 1.准备 pom.xml <dependency> <groupId>org.apache. ...

  5. Delphi DbgridEh实现鼠标拖动选中列,并使复选框选中

    1.先设置表格列的属性 procedure TForm_TaskToDW.InitGrid;var  MyCol: TColumnEh;begin  with DBGridEh_Task do  be ...

  6. 【HANA系列】SAP HANA STUDIO客户端升级更新

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA STUDIO客 ...

  7. HDU4372(第一类斯特林数)

    题意:N座高楼,高度均不同且为1~N中的数,从前向后看能看到F个,从后向前看能看到B个,问有多少种可能的排列数. 0 < N, F, B <= 2000 首先我们知道一个结论:n的环排列的 ...

  8. python+selenium下弹窗alter对象处理01

    alt.accept() :                            等同于单击“确认”或者“OK” alt.dismiss() :                            ...

  9. C#方法名前的方括号

    1.序列化:[Serializable]public void 方法名(){...} 2.WebServices方法:[WebMethod]public void 方法名(){...} 3.Ajax( ...

  10. Simplify Path(路径简化)

    问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...