vue.js怎么请求数据


本文摘自PHP中文网,作者coldplay.xixi,侵删。

vue.js请求数据的方法:首先安装【vue-resource】模块;然后在【main.js】引入【vue-resource】,并在组件里面直接使用。

【相关文章推荐:vue.js】

vue.js请求数据的方法:

一,vue-resource请求数据

介绍:vue-resource请求数据方式是官方提供的一个插件

使用步骤:

1、安装vue-resource模块

1

cnpm install vue-resource --save

加--save是为了在package.json中引用,表示在生产环境中使用。因为我们在日常开发中,如果我们要打包代码给其他人或者上传到github,又或者要发布代码时,package.json就是安装所需要的包。如果只在开发环境中使用,则只需要--save-dev,有一些只在开发环境中用,有一些要在生产环境中用。

2、在 main.js 引入 vue-resource

1

2

import VueResource from 'vue-resource';

Vue.use(VueResource);

3、在组件里面直接使用

1

2

3

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

  

})

注意:this.$http.get()等等的各种http请求都是继承promise的。promise是异步的请求;其次,.then箭头函数里的this代表的是上下文。根据箭头函数this的定义,只在函数定义时就已经赋值可知,this,指代的是定义函数的对象,在vue中对象就是methods当前页面。所以this指导的是data里面的数据。如果想要获取包裹函数外函数的数据,即闭包的概念。实现方法就是在外层函数加一个var that = this;将外层的this先储存到that中。

实例:

Info.vue

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<template>

  <div id="info">

    <button @click="getData">获取数据</button>

    <ul>

      <li v-for="(item,index) in list" v-bind:key="index">

        {{item.title}}

      </li>

    </ul>

  </div>

</template>

  

<script>

  export default {

    name: "Info",

    data() {

      return {

        list: []

      }

    },

    methods: {

      getData: function () {

        let 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() {

      this.getData();

    }

  }

</script>

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

1

2

3

4

5

6

7

8

9

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请求数据

介绍:这是一个第三方的插件 github地址:https://github.com/axios/axios

axios 与 fetch-jsonp 同为第三方插件

1、安装

1

cnpm install axios --save

直接调用。和vue-resource的区别是:aixos是每在一个页面用一次就要在该页面调用一次。vue-resource是绑定了全局的了。

2、哪里用哪里引入axios

1

2

3

4

5

Axios.get(api).then((response)=>{

  this.list=response.data.result;

}).catch((error)=>{

  console.log(error);

})

关于axios的跨域请求

在config->index.js->proxyTable配置如下:target填写自己想要的地址

262b908e44f4711b932cc7fe614dacf.png

如下配置,url为地址后面所带的参数,配置好后,现在npm run dev 运行就行。

057c32d3e7d8def105a5b4d1d3252e1.png

关于多个并发请求:

7f484f0a16b43d36913bd15208545ad.png

上面这个是同一地址的跨域,如果要拿不同地址的跨域,只需要更改config->index.js->proxyTable的配置,增加地址块就行。

三,关于fetch-jsonp

github地址:https://github.com/camsong/fetch-jsonp

1、安装

1

cnpm install fetch-jsonp --save

2、哪里用哪里引入fetch-jsonp

1

2

3

4

5

6

7

8

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)

 })

相关免费学习推荐:JavaScript(视频)

以上就是vue.js怎么请求数据的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

vue.js图片资源路径不正确怎么办

浅谈vue.use到底是什么?

vue.js如何遍历map

vue.js怎么判断对象为空

vue.js是前端还是后端

vue.js打包后图片路径错误怎么办

vue.js全家桶是什么

如何做到刷新vue.js改变数据

vue.js的ul-li标签仿select标签

vue.js支持jquery吗?

更多相关阅读请进入《vue.js》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    暂无评论...