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. 阶段3 1.Mybatis_11.Mybatis的缓存_1 今日课程安排

  2. 测开之路一百五十二:基于jquery的ajax实现之load、get、ajax

    ajax除了用原生的js实现之外,也可以使用jquery实现,而且用jquery更方便 看一个简单的示例,保留上一篇的content路由和html,实现上一篇一样的功能,点击获取内容,局部刷新 准备一 ...

  3. powered by Fernflower decompiler

    About Fernflower Fernflower is the first actually working analytical decompiler for Java and probabl ...

  4. spark MLlib矩阵四则运算,线性代数

    1.导包请看我的上一篇博文,maven项目的包 https://www.cnblogs.com/wuzaipei/p/10965680.html 2.denseMatirx 矩阵四则运算如下 版本不同 ...

  5. Java基础/时间日期格式

    Java时间日期格式转换 一.Date转String和String转Date 参考博客:https://www.cnblogs.com/sharpest/p/7879377.html public s ...

  6. .net core 学习小结之 配置介绍(config)以及热更新

    命令行的配置 var settings = new Dictionary<string, string>{ { "name","cyao"}, {& ...

  7. Redis在Windows环境配置多实例多端口运行

    Redis是一种跨平台NoSql内存数据库,这几年各大公司系统应用中非常多见,支持多种数据类型,可以持久化保存数据,很多公司直接拿来作为数据库使用. 相比于Memcached,Redis支持持久化保存 ...

  8. Phone List POJ-3630 字典树 or 暴力

    Phone List POJ-3630 字典树 or 暴力 题意 目前有 t 组数据, n 个电话号码,如果拨打号码的时候 先拨通了某个号码,那么这一串号码就无法全部拨通. 举个例子 911 和 91 ...

  9. SCUT - 482 - 生成树上的点 - Prufer

    https://scut.online/p/482 没听说过这个东西. 洛谷也有这个,所以还是要去接触一些奇奇怪怪的知识才行. https://www.luogu.org/problem/P2290 ...

  10. spring data jpa Specification 复杂查询+分页查询

    当Repository接口继承了JpaSpecificationExecutor后,我们就可以使用如下接口进行分页查询: /** * Returns a {@link Page} of entitie ...