vue请求数据
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请求数据的更多相关文章
- vue 使用 jsonp 请求数据
vue 使用 jsonp 请求数据 vue请求数据的时候,会遇到跨域问题,服务器为了保证信息的安全,对跨域请求进行拦截,因此,为了解决vue跨域请求问题,需要使用jsonp. 安装jsonp npm ...
- vue 对象提供的属性功能、通过axio请求数据(2)
1 Vue对象提供的属性功能 1.1 过滤器 过滤器,就是vue允许开发者自定义的文本格式化函数,可以使用在两个地方:输出内容和操作数据中. 1.1.1 使用Vue.filter()进行全局定义(全局 ...
- vue axios数据请求get、post方法的使用
我们常用的有get方法以及post方法,下面简单的介绍一下这两种请求方法 vue中使用axios方法我们先安装axios这个方法 npm install --save axios 安装之后采用按需引入 ...
- 前端向服务器请求数据并渲染的方式(ajax/jQuery/axios/vue)
原理: jQuery的ajax请求:complete函数一般无论服务器有无数据返回都会显示(成功或者失败都显示数据): return result
- $Django 前后端之 跨域问题(同源策略) vue项目(axios跨域请求数据)
1 跨域问题(多个域之间的数据访问) #同源策略(ip port 协议全部相同) #本站的只能请求本站域名的数据 #CORS实现(跨域资源共享) #实现CORS通信的关键是服务器.只要服务器实现了CO ...
- vue使用element Transfer 穿梭框实现ajax请求数据和自定义查询
vue使用element Transfer 穿梭框实现ajax请求数据和自定义查询 基于element Transfer http://element-cn.eleme.io/#/zh-CN/comp ...
- Vue之单文件组件的数据传递,axios请求数据及路由router
1.传递数据 例如,我们希望把父组件的数据传递给子组件. 可以通过props属性来进行传递. 传递数据三个步骤: 步骤1:在父组件中,调用子组件的组名处,使用属性值的方式往下传递数据 <Menu ...
- vue vue-resource 请求数据
main.js import Vue from 'vue'; import App from './App.vue'; /*使用vue-resource请求数据的步骤 1.需要安装vue-resour ...
- vue 请求后台数据2(copy)
https://blog.csdn.net/vergilgeekopen/article/details/68954940 需要引用vue-resource 安装请参考https://github.c ...
随机推荐
- centos官网镜像下载方法
1.CentoS简介 CentOS(Community Enterprise Operating System,社区企业操作系统)是一个基于Red Hat Linux 提供的可自由使用源代码的企业级L ...
- 七:flask-一些小细节
1.在局域网中,让其他电脑访问我的网站:host参数 如果设置为0.0.0.0,则在局域网中,输入当前项目所在的ip+端口就可以访问这个项目如果host设置为固定的ip,如host=‘'192.168 ...
- list,string,tuple,dictionary之间的转换
list,string,tuple,dictionary之间的转换 类型 String List tuple dictionary String - list(str), str.split() tu ...
- 《Using Python to Access Web Data》 Week5 Web Services and XML 课堂笔记
Coursera课程<Using Python to Access Web Data> 密歇根大学 Week5 Web Services and XML 13.1 Data on the ...
- Lesson 2 Thirteen equals one
vicar 牧师 grocer 杂货铺店主 with a start 由于受到惊吓 Whtaever are you dong up here?你究竟在这上面干什么?whatever用于疑问句中,用以 ...
- python+selenium模拟键盘输入
from selenium.webdriver.common.keys import Keys #键盘导入类 --------------------------------------------- ...
- JSP总结(jsp/EL表达式/核心标签)
1.概念 jsp,即java Server Pages,java服务器页面. 2.简单介绍 小示例 <%@ page language="java" contentType= ...
- 【暑假培训1】test1
T1: 30pts:直接暴力三层循环枚举 就就就先不写代码了qwq: 70pts: 因为X+Y+Z==0 所以我们可以考虑枚举X和Y,然后利用↑式子求出Z=-X-Y: 然后touli xcg的70pt ...
- 1、Java语言概述与开发环境——编译和运行第一个程序HelloWorld.java
编写一个Java程序到运行的步骤概述: 1.将Java代码编写到扩展名为.Java的文件中 2.通过Javac命令对该Java文件进行编译 3.通过Java命令对生成的class文件进行运行 一.编写 ...
- HDU-4857 逃生(反向拓扑排序 + 逆向输出)
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...