3.vue请求数据

请求数据的方式:

  • vue-resource 官方提供的 vue的一个插件
  • axios
  • fetch-jsonp

vue-resource的使用

使用步骤:
1、安装vue-resource模块

cnpm install vue-resource --save  

2、在 main.js 引入 vue-resource

import VueResource from 'vue-resource';
Vue.use(VueResource);

3、在组件里面直接使用

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

})

实例:
Info.vue



如果getData()中不适用箭头函数,就需要注意this问题。

getData: function () {
    let api = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1';
    const _this = this;
    this.$http.get(api).then(function (res) {
        _this.list = res.body.result;
    }, function (err) {
        console.log(err);
    });
}

axios 的使用

axios 与 fetch-jsonp 同为第三方插件
1、安装

cnpm  install  axios --save

2、哪里用哪里引入axios

Axios.get(api).then((response)=>{
    this.list=response.data.result;
}).catch((error)=>{
    console.log(error);
})

fetch-jsonp 的使用

1、安装

cnpm  install  fetch-jsonp --save

2、哪里用哪里引入axios

fetchJsonp('/users.jsonp')
  .then(function(response) {
    return response.json()
  }).then(function(json) {
    console.log('parsed json', json)
  }).catch(function(ex) {
    console.log('parsing failed', ex)
  })

你可能感兴趣的:(前端,vue-resource,vue.js,axios)