vue基础:axios请求数据,vue-resource请求数据

vue基础:axios请求数据,vue-resource请求数据

一:axios请求数据完整

1.安装命令:

npm install axios --save

2.导入,在哪用就在哪导入

import Axios from 'axios';

3.请求数据

<script>
import Axios from 'axios';
export default {
    data(){
        return{
            list:[]
        }
    },methods:{
        getdata(){
            var api='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1';
            Axios.get(api).then((response)=>{
                    console.log(response);
                    this.list=response.data.result;
            }).catch((error)=>{
                console.log(error);
            });
        }
    }
}
</script>

二:vue-resource请求数据完整

官方提供的一个插件。
1.安装

npm install vue-resource --save

2.在mian.js引入

import VueResource from 'vue-resource'
Vue.use(VueResource);//使用插件

3.请求数据

<script>
export default {
    data(){
        return{

        }
    },methods:{
        getdata(){
            var api='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1';
            this.$http.gt(api).then(function(response){
                console.log(response);
            },function(error){
                console.log(error);
            })
        }
    }
}
</script>
发布了51 篇原创文章 · 获赞 45 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/a1424261303/article/details/86575125
今日推荐