vue http请求 vue-resource使用方法

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

2、在main.js中引入

import http from ‘vue-resource‘

3、使用方法

// 基于全局Vue对象使用http
Vue.http.get(‘/someUrl‘, [options]).then(successCallback, errorCallback);
Vue.http.post(‘/someUrl‘, [body], [options]).then(successCallback, errorCallback);

// 在一个Vue实例内使用$http
this.$http.get(‘/someUrl‘, [options]).then(successCallback, errorCallback);
this.$http.post(‘/someUrl‘, [body], [options]).then(successCallback, errorCallback);

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

store.js 代码

import Vue from ‘vue‘
import Vuex from ‘vuex‘

Vue.use(Vuex)

// 定义初始值
const state = {
    isShowLoading: false
}

// 获取变量值
const getters = {
    isShowLoading: state => state.isShowLoading
}

//定义触发状态对象方法,传入state整个对象
//在页面中触发时使用this.$store.commit(‘mutationName‘) 触发Mutations方法改变state的值
const mutations = {
    setLoadingType(state, type) {
      state.isShowLoading = type;
    }
}

//异步执行方法,传入参数context,等同于整个store
//处理Mutations中已经写好的方法 其直接触发方式是 this.$store.dispatch(actionName)
const actions = {
    setLoadingType({commit}, type) {
        // 调用mutations 方法
        commit(‘setLoadingType‘, type)
    }
}

export default new Vuex.Store({
    state,
    mutations,
    actions,
    getters
})

main.js 代码

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from ‘vue‘
import App from ‘./App‘
import router from ‘./router‘
import http from ‘vue-resource‘
import $ from ‘jquery‘
// 引入sotre.js
import store from ‘./components/common/store.js‘

Vue.config.productionTip = false

Vue.use(http)

Vue.http.interceptors.push((request, next) => {
	// 也可以再这里验证是否登录等操作
	// 显示loading
    store.dispatch(‘setLoadingType‘, true);
    next((response) => {
        // 隐藏loading
  	    store.dispatch(‘setLoadingType‘, false);
        return response
    });
});

/* eslint-disable no-new */
new Vue({
    store,
    el: ‘#app‘,
    router,
    render: h => h(App)
});

  

新建Loading.vue

<template id="loading-template" class="loading">
  <div class="loading-overlay">
    <div class="sk-three-bounce">
      <div class="sk-child sk-bounce1"></div>
      <div class="sk-child sk-bounce2"></div>
      <div class="sk-child sk-bounce3"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: ‘loading‘,
  data () {
    return {
      msg: ‘this.test uve‘
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}
.loading-overlay {
  content: "";
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  opacity: 1;
  background: rgba(0, 0, 0, 0.5);
  transition: all .6s;
}
</style>

App.vue 添加代码

<template>
    <div id="app">
        <div id="help">
            <loading v-show="isShowLoading"></loading>
        </div>
        <router-link to="/Login">跳转到详情页</router-link>
        <img src="./assets/logo.png">
        <router-view></router-view>
  </div>

</template>
<script>

import loading from ‘./components/Loading‘
import {mapGetters} from ‘vuex‘
export default {
    name: ‘app‘,
    components:{
        loading
    },
    data () {
        return {
        }
    },
    //computed 实时计算 Vue检测到数据发生变动时就会执行对相应数据有引用的函数。
    computed: {
        ...mapGetters([
            ‘isShowLoading‘
        ])
    }
}
</script>

<style>
#app {
  font-family: ‘Avenir‘, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

  

时间: 2024-08-30 11:22:00

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

vue http请求 vue自带的 vue-resource

vue-resource安装 npm install vue-resource --save-dev 配置 在main.js中引入插件 //Resource 为自定义名 vue-resource 为插件的名字 import Vue from 'vue' import Resource from 'vue-resource' 注册使用 //注册使用 vue-router Vue.use(Router) 使用   跳转了解钩子函数 //created钩子函数 函数执行时 ----- 组件实例化完毕,

vue resource 携带cookie请求 vue cookie 跨域

vue resource 携带cookie请求 vue cookie 跨域 1.依赖VueResource 确保已安装vue-resource到项目中,找到当前项目,命令行输入: npm install vue-resource --save 在主方法添加 过滤 Vue.http.interceptors.push(function(request, next) {//拦截器 // 跨域携带cookie request.credentials = true; next() }) 以下是针对每个请

Vue数据请求 axios vs fetch

Vue数据请求 数据请求在前端开发中的使用有两种形式 使用原生javascript提供的数据请求 ajax( 四部曲,一般需要我们结合Promise去封装,使用不是很便利,但是效率很高 ) fetch( 本身结合了Promise,并且已经做好了封装,可以直接使用 ) 使用格式: 使用别人封装好的第三方库目前最流行的,使用率最高的是 axios vue中我们最常使用的 vue 1.x 的版本提供了一个封装库 vue-resource , 但是到了vue 2.x版本之后,这个就弃用了vue-reso

Vue创建实例,数据与方法

一.创建Vue实例 每个Vue应用都是通过Vue函数创建一个新的Vue实例开始的: var vm = new Vue({ //......... }) 虽然没有完全遵循 MVVM 模型,但是 Vue 的设计也受到了它的启发.因此在文档中经常会使用 vm (ViewModel 的缩写) 这个变量名表示 Vue 实例. 二.数据与方法 当一个 Vue 实例被创建时,它向 Vue 的响应式系统中加入了其 data 对象中能找到的所有的属性.当这些属性的值发生改变时,视图将会产生"响应",即匹

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

vue 和 react 组件间通信方法对比 通信路径 vue的方法 react的方法 父组件 => 子组件 props(推荐).this.$refs.this.$children props 子组件 => 父组件 自定义事件($emit.$on)(推荐).this.$parent 回调函数(推荐) 非父子组件(兄弟组件.跨级组件) bus 高阶组件(推荐).自定义事件.context 原文地址:https://www.cnblogs.com/cag2050/p/9054840.html

vue调试工具vue-devtools安装及使用方法

vue调试工具vue-devtools安装及使用方法 https://www.jb51.net/article/150335.htm 本文主要介绍 vue的调试工具 vue-devtools 的安装和使用 工欲善其事, 必先利其器, 快快一起来用vue-devtools来调试开发你的vue项目吧 安装: 1.到github下载: ? 1 git clone https://github.com/vuejs/vue-devtools 2.在vue-devtools目录下安装依赖包 ? 1 2 cd

vue超简单加载字体方法,解决scss难加载字体的问题

vue超简单加载字体方法,解决scss难加载字体的问题 scss在加载字体方面一直不太好用,需要繁杂的配置才能达到想要的效果,这里说一种非常简单的方法 在App.vue的style标签下引入字体文件后,scss设置的字体依旧可以正确识别,注意style的lang不要写,就使用原生css 示例引入 <style> @font-face{ font-family: pingfang; src: url('./style/pingfang.ttf') } </style> 然后想要的位置

axios发送两次请求原因及解决方法

axios发送两次请求原因及解决方法 最近Vue项目中使用axios组件,在页面交互中发现axios会发送两次请求,一种请求方式为OPTIONS,另外一种为自己设置的. 如图: 什么是CORS通信? CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而克服了AJAX只能同源使用的限制. CORS需要浏览器和服务器同时支持.目前,所有浏览器都支持该功能,IE浏

Vue.js起手式+Vue小作品实战

本文是小羊根据Vue.js文档进行解读的第一篇文章,主要内容涵盖Vue.js的基础部分的知识的,文章顺序基本按照官方文档的顺序,每个知识点现附上代码,然后根据代码给予个人的一些理解,最后还放上在线编辑的代码以供练习和测试之用:在最后,我参考SegmentFault上的一篇技博,对Vue进行初入的实战,目的是将新鲜学到的知识立即派上用场:如果你还是前端的小白,相信这篇文章可能会对产生一些帮助和引起思想的碰撞,因为大家的学习历程是相似的,遇到的困惑也有一定的共通性,如果文章出现谬误之处,欢迎各位童鞋