VUE的网络请求

一:
1.在cmd中使用npm 下载插件: cnpm install vue-resource --save

2.在main.js中引入vue-resource:

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

3.在组件中直接使用

    methods:{
        getListData(){//网络请求数据
            var url = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1";
             //jsonp请求,需要后台接口支持jsonp
             // this.$http.jsonp(api).then((response)=>{
            //get请求
            this.$http.get(url).then((response)=>{
                console.log("请求到的数据:"+response);
                this.list = response.body.result;
            },(error)=>{
                console.log("请求错误:"+error);
            })
        },
    },
    mounted(){//模板已经编译 -- 执行请求数据的操作
        this.getListData();
    }

二:
使用VUE的官网的axios插件请求,

  1. 安装
 cnpm  install  axios --save

2.每一个地方使用网络请求,就在那个地方引入axios,然后直接用就行

  import Axios from 'axios';

3.使用

         var url = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1";
            Axios.get(url).then((response)=>{
                    this.msglist=response.data.result;
                    console.log("请求到的数据66666:"+response);
                }).catch((error)=>{
                    console.log(error);
                })
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容