1、安装vue-resource扩展: npm install vue-resource

2、在main.js中引入

  1. import http from 'vue-resource'

3、使用方法

  1. // 基于全局Vue对象使用http
  2. Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);
  3. Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
  4.  
  5. // 在一个Vue实例内使用$http
  6. this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);
  7. this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);

4、使用拦截器显示和隐藏loading效果 (需要用到vuex扩展,vuex使用方法戳这里

store.js 代码

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3.  
  4. Vue.use(Vuex)
  5.  
  6. // 定义初始值
  7. const state = {
  8. isShowLoading: false
  9. }
  10.  
  11. // 获取变量值
  12. const getters = {
  13. isShowLoading: state => state.isShowLoading
  14. }
  15.  
  16. //定义触发状态对象方法,传入state整个对象
  17. //在页面中触发时使用this.$store.commit('mutationName') 触发Mutations方法改变state的值
  18. const mutations = {
  19. setLoadingType(state, type) {
  20. state.isShowLoading = type;
  21. }
  22. }
  23.  
  24. //异步执行方法,传入参数context,等同于整个store
  25. //处理Mutations中已经写好的方法 其直接触发方式是 this.$store.dispatch(actionName)
  26. const actions = {
  27. setLoadingType({commit}, type) {
  28. // 调用mutations 方法
  29. commit('setLoadingType', type)
  30. }
  31. }
  32.  
  33. export default new Vuex.Store({
  34. state,
  35. mutations,
  36. actions,
  37. getters
  38. })

main.js 代码

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. import http from 'vue-resource'
  7. import $ from 'jquery'
  8. // 引入sotre.js
  9. import store from './components/common/store.js'
  10.  
  11. Vue.config.productionTip = false
  12.  
  13. Vue.use(http)
  14.  
  15. Vue.http.interceptors.push((request, next) => {
  16. // 也可以再这里验证是否登录等操作
  17. // 显示loading
  18. store.dispatch('setLoadingType', true);
  19. next((response) => {
  20. // 隐藏loading
  21. store.dispatch('setLoadingType', false);
  22. return response
  23. });
  24. });
  25.  
  26. /* eslint-disable no-new */
  27. new Vue({
  28. store,
  29. el: '#app',
  30. router,
  31. render: h => h(App)
  32. });

  

新建Loading.vue

  1. <template id="loading-template" class="loading">
  2. <div class="loading-overlay">
  3. <div class="sk-three-bounce">
  4. <div class="sk-child sk-bounce1"></div>
  5. <div class="sk-child sk-bounce2"></div>
  6. <div class="sk-child sk-bounce3"></div>
  7. </div>
  8. </div>
  9. </template>
  10.  
  11. <script>
  12. export default {
  13. name: 'loading',
  14. data () {
  15. return {
  16. msg: 'this.test uve'
  17. }
  18. }
  19. }
  20. </script>
  21.  
  22. <!-- Add "scoped" attribute to limit CSS to this component only -->
  23. <style scoped>
  24. h1, h2 {
  25. font-weight: normal;
  26. }
  27.  
  28. ul {
  29. list-style-type: none;
  30. padding: 0;
  31. }
  32.  
  33. li {
  34. display: inline-block;
  35. margin: 0 10px;
  36. }
  37.  
  38. a {
  39. color: #42b983;
  40. }
  41. .loading-overlay {
  42. content: "";
  43. position: fixed;
  44. top: 0;
  45. left: 0;
  46. right: 0;
  47. bottom: 0;
  48. z-index: 1000;
  49. opacity: 1;
  50. background: rgba(0, 0, 0, 0.5);
  51. transition: all .6s;
  52. }
  53. </style>

App.vue 添加代码

  1. <template>
  2. <div id="app">
  3. <div id="help">
  4. <loading v-show="isShowLoading"></loading>
  5. </div>
  6. <router-link to="/Login">跳转到详情页</router-link>
  7. <img src="./assets/logo.png">
  8. <router-view></router-view>
  9. </div>
  10.  
  11. </template>
  12. <script>
  13.  
  14. import loading from './components/Loading'
  15. import {mapGetters} from 'vuex'
  16. export default {
  17. name: 'app',
  18. components:{
  19. loading
  20. },
  21. data () {
  22. return {
  23. }
  24. },
  25. //computed 实时计算 Vue检测到数据发生变动时就会执行对相应数据有引用的函数。
  26. computed: {
  27. ...mapGetters([
  28. 'isShowLoading'
  29. ])
  30. }
  31. }
  32. </script>
  33.  
  34. <style>
  35. #app {
  36. font-family: 'Avenir', Helvetica, Arial, sans-serif;
  37. -webkit-font-smoothing: antialiased;
  38. -moz-osx-font-smoothing: grayscale;
  39. text-align: center;
  40. color: #2c3e50;
  41. margin-top: 60px;
  42. }
  43. </style>

  

vue http请求 vue-resource使用方法的更多相关文章

  1. vue http请求 vue自带的 vue-resource

    vue-resource安装 npm install vue-resource --save-dev 配置 在main.js中引入插件 //Resource 为自定义名 vue-resource 为插 ...

  2. vue resource 携带cookie请求 vue cookie 跨域

    vue resource 携带cookie请求 vue cookie 跨域 1.依赖VueResource 确保已安装vue-resource到项目中,找到当前项目,命令行输入: npm instal ...

  3. vue开发请求本地模拟数据的配置方法(转)

    VUE开发请求本地数据的配置,早期的vue-lic下面有dev-server.js和dev-client.js两文件,请求本地数据在dev-server.js里配置,最新的vue-webpack-te ...

  4. VUE 数据请求和响应(axios)

    1. 概述 1.1 简介 axios是一个基于Promise(本机支持ES6 Promise实现) 的HTTP库,用于浏览器和 nodejs 的 HTTP 客户端.具有以下特征: 从浏览器中创建 XM ...

  5. 手把手教你vue配置请求本地json数据

    本篇文章主要介绍了vue配置请求本地json数据的方法,分享给大家,具体如下:在build文件夹下找到webpack.dev.conf.js文件,在const portfinder = require ...

  6. VUE开发请求本地数据的配置,旧版本dev-server.js,新版本webpack.dev.conf.js

    VUE开发请求本地数据的配置,早期的vue-lic下面有dev-server.js和dev-client.js两文件,请求本地数据在dev-server.js里配置,最新的vue-webpack-te ...

  7. vue 和 react 组件间通信方法对比

    vue 和 react 组件间通信方法对比: 通信路径 vue的方法 react的方法 父组件 => 子组件 props(推荐).slot(推荐).this.$refs.this.$childr ...

  8. vue教程2-04 vue实例简单方法

    vue教程2-04 vue实例简单方法 vue实例简单方法: vm.$el -> 就是元素 vm.$data -> 就是data <!DOCTYPE html> <htm ...

  9. Vue 网络请求

    Vue网络请求,用的是vue-resource 1. 首先需要安装vue-resource npm install vue-resource 2. 安装好之后,会在package.json文件中自动加 ...

随机推荐

  1. FusionCharts Free 甘特图

    用FusionCharts做甘特图 1.同步方式(用xml格式字符) 前台aspx代码 <!DOCTYPE html> <html xmlns="http://www.w3 ...

  2. SDOI2017 Round1 Day2 题解

    T2好厉害啊……AK不了啦……不过要是SCOI考这套题就好了240保底. BZOJ4819 新生舞会 模板题,分数规划+二分图最大权匹配. 费用流跑得过,可以不用KM. UPD:分数规划用迭代跑得飞快 ...

  3. [HNOI 2010] 弹飞绵羊

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2002 [算法] LCT动态维护森林连通性 时间复杂度 : O(NlogN ^ 2) ...

  4. C++可移植性和跨平台初探

    概述 今天聊聊C++的可移植性问题.如果你平时使用C++进行开发,并且你对C++的可移植性问题不是非常清楚,那么我建议你看看这个系列.即使你目前没有跨平台开发的需要,了解可移植性方面的知识对你还是很有 ...

  5. MVC错误:查询的结果不能枚举多次

    应用程序中的服务器错误. 查询的结果不能枚举多次. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: S ...

  6. CentOS6.6 zookeeper完全集群搭建

    centos6.6搭建zookeeper-3.4.6完全分布式环境 转载 2015-06-28 22:14:17 标签:it 为了搭建HBase完全分布式环境,前提就是搭建好zookeeper和Had ...

  7. Android开发--Intent

    一:Intent基础知识 Intent:信使,实现四大组件间的通信. 1:实现页面间的跳转,有两种方式: (1):startActivity()方式: (2) 2:使用Intent传递数据的四种方式: ...

  8. mongodb和mysql语法对比

    MySQL: SELECT * FROM user Mongo: db.user.find() —————————————— MySQl: SELECT * FROM user WHERE name ...

  9. 11.ClientCredential模式总结

    11.ClientCredential模式总结 服务端定义的Resource叫做api Resource API默认是被保护的 第三方的客户端先去请求 Server拿到access token.带着t ...

  10. VS Supercharger插件

    一.前言 Supercharger 是 VS 的一款插件,针对代码进行优化和着色,便于观察和区分. 二.下载及安装 下载的 URL 如下:Supercharger 下载地址 点击下载,下载完成以后,那 ...