<template>
<div>
<h2>请求组件</h2>
<button @click="getData()">请求数据</button>
</div>
</template>
<script>
/*
请求数据的模板
vue-resource 官方提供的vue 的一个插件
1.安装 npm install vue-resource --save
2.在main.js中注册该模块
import VueResource from 'vue-resource'
Vue.use(VueResource);
3.在组件中直接使用
this.$http.get(地址).then(function(success_response){},finction(err_response){})
axios
1.安装
npm install axios --save
2.哪里用哪里引用
import axios from 'axios';
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
或者
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
fetch-jsonp
与axios一致
*/
export default {
name: "qingqiu",
methods:{
getData(){
var _this=this;
var api='http://www.phcnegap100.com/appapi/php?a=getProtalList&catid=20&page=1'
this.$http.get(api).then(response => {
//根据服务器相应的数据改变modle数据时,因闭包原因要使用上面的_this去改变
// get body data
console.log("请求成功");
console.log(response);
}, response => {
// error callback
console.log("请求失败");
console.log(response);
});
}
}
}
</script>
<style scoped>
</style>
VUE-请求数据的三种插件
最新推荐文章于 2025-05-08 14:19:03 发布