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)
})
}

最新文章

  1. Socket通信(一)
  2. C#的反射机制
  3. c++ 语言细节
  4. Android实例-拍摄和分享照片、分享文本(XE8+小米2)
  5. 百度编辑器ueditor 在vs2008中的使用方法
  6. 效果网址http://sc.chinaz.com/tag_jiaoben/tupianlunbo.html
  7. vs2010 入门程序
  8. 关于PHP包含文件的方法
  9. 简述angular自定义过滤器在页面和控制器中的使用
  10. 【转载】给想要入门渗透的人的忠告——schiz0wcingU
  11. c#学习笔记 day_one
  12. 1.python简介
  13. Exception和解决方案
  14. php7 date函数警告去除
  15. requirejs 使用实例r.js打包
  16. mac os下不同工具go env下gopath显示不同
  17. Centos 7 下 LAMP 部署
  18. Nginx+Tomcat安装与配置(windows版)
  19. 编写无Java脚本的JSP页面
  20. cf900D. Unusual Sequences(容斥 莫比乌斯反演)

热门文章

  1. PADS LAYOUT的一般流程
  2. IIS安全狗问题
  3. SpringContextHolder使用报错
  4. JMeter常用的4种参数化方式-操作解析
  5. Discrete Mathematics and Its Applications | 1 CHAPTER The Foundations: Logic and Proofs | 1.1 Propositional Logic
  6. Java String == &amp;&amp; equal
  7. axios入门使用
  8. TortoiseSVN-1.9.7 对应 eclipse svn 插件的 更新
  9. Linux hostname 主机名篇
  10. Element-ui 使用详细介绍