当前位置:   article > 正文

vue中http请求_vue.http

vue.http

前后端采用异步接口进行数据交互,传统情况下,我们采用jquery中的$.ajax()方法来进行异步请求。而在vue中我们并没有引入jquery而是采用了vue的插件vue-resource,它同样对异步请求进行了封装,方便我们同服务端进行数据交互。

1、引用及使用

cnpm install vue-resource --save-dev 

在项目根目录下/src/main.js中:

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

此时在vue文件中就可以使用this.$http()进行网络请求。

2、通常在项目中是需要将http请求进行封装的。

为了封装成全局因此使用vuex。

参照 vue的一些总结 这个文章进行配置vuex

配置成功后,将main.js中的VueResource内容去掉,写在vuex中的store.js中

store.js内容如下

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import VueResource from 'vue-resource'
  4. Vue.use(Vuex)
  5. Vue.use(VueResource)
  6. export default new Vuex.Store({
  7. state: {
  8. url: 'www.baidu.com'
  9. },
  10. mutations: {
  11. setUrl (state, res) {
  12. // 该方法用来改变全局变量的值
  13. state.url = res
  14. }
  15. }
  16. })

3、在store.js中封装http请求的方法

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import VueResource from 'vue-resource'
  4. Vue.use(Vuex)
  5. Vue.use(VueResource)
  6. export default new Vuex.Store({
  7. state: {
  8. // url: 'www.baidu.com'
  9. },
  10. mutations: {
  11. // setUrl (state, res) {
  12. // // 该方法用来改变全局变量的值
  13. // state.url = res
  14. // },
  15. get(state, data) {
  16. Vue.http({
  17. method: 'GET',
  18. url: data.url,
  19. emulateJSON: true
  20. })
  21. .then(function (success) {
  22. // console.log("成功",success)
  23. data.success(success.body);
  24. }, function (error) {
  25. // console.log("错误",error)
  26. data.error(error.body);
  27. })
  28. /*
  29. Vue.http.get("http://test.dcsf.hebeiwanteng.com/api/common/wxParking/getNowTime")
  30. .then((success)=>{
  31. console.log("成功",success)
  32. })
  33. .catch((error)=>{
  34. console.log("失败",error)
  35. })
  36. */
  37. },
  38. post(state, data) {
  39. Vue.http({
  40. method: 'POST',
  41. url: data.url,
  42. params: data.data,
  43. emulateJSON: true
  44. }).then(function (success) {
  45. data.success(success.body);
  46. }, function (error) {
  47. data.error(error.body);
  48. })
  49. },
  50. delete(state, data) {
  51. Vue.http({
  52. method: 'DELETE',
  53. url: data.url,
  54. params: data.data,
  55. emulateJSON: true
  56. }).then(function (success) {
  57. data.success(success.body);
  58. }, function (error) {
  59. data.error(error.body);
  60. })
  61. },
  62. put(state, data) {
  63. Vue.http({
  64. method: 'PUT',
  65. url: data.url,
  66. params: data.data,
  67. emulateJSON: true
  68. }).then(function (success) {
  69. data.success(success.body);
  70. }, function (error) {
  71. data.error(error.body);
  72. })
  73. },
  74. }
  75. })

此时封装好了http请求。

在vue的文件中调用:

<script>
  import { mapState, mapMutations } from 'vuex'
  export default {
    name: 'HelloWorld',
    data () {
      return {
      }
    },
    methods: {
      ...mapMutations(['get','post','delete','put']), //引入方法
    },
    created() {
      let _this = this;
      _this.get({
        url:"<span style="color:#ff0000;">此处写请求地址</span>",
        success(success) {
          console.log("成功",success)
        },
        error(error) {
          console.log("错误",error)
        }
      });
    }
  }
</script>

4、注意:在store.js中使用Vue.http和在vue文件中使用this.$http是一样的。

5、该方法只验证了get请求,其它请求没有做验证。


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号