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>
vue http请求 vue-resource使用方法的更多相关文章
- vue http请求 vue自带的 vue-resource
vue-resource安装 npm install vue-resource --save-dev 配置 在main.js中引入插件 //Resource 为自定义名 vue-resource 为插 ...
- vue resource 携带cookie请求 vue cookie 跨域
vue resource 携带cookie请求 vue cookie 跨域 1.依赖VueResource 确保已安装vue-resource到项目中,找到当前项目,命令行输入: npm instal ...
- vue开发请求本地模拟数据的配置方法(转)
VUE开发请求本地数据的配置,早期的vue-lic下面有dev-server.js和dev-client.js两文件,请求本地数据在dev-server.js里配置,最新的vue-webpack-te ...
- VUE 数据请求和响应(axios)
1. 概述 1.1 简介 axios是一个基于Promise(本机支持ES6 Promise实现) 的HTTP库,用于浏览器和 nodejs 的 HTTP 客户端.具有以下特征: 从浏览器中创建 XM ...
- 手把手教你vue配置请求本地json数据
本篇文章主要介绍了vue配置请求本地json数据的方法,分享给大家,具体如下:在build文件夹下找到webpack.dev.conf.js文件,在const portfinder = require ...
- VUE开发请求本地数据的配置,旧版本dev-server.js,新版本webpack.dev.conf.js
VUE开发请求本地数据的配置,早期的vue-lic下面有dev-server.js和dev-client.js两文件,请求本地数据在dev-server.js里配置,最新的vue-webpack-te ...
- vue 和 react 组件间通信方法对比
vue 和 react 组件间通信方法对比: 通信路径 vue的方法 react的方法 父组件 => 子组件 props(推荐).slot(推荐).this.$refs.this.$childr ...
- vue教程2-04 vue实例简单方法
vue教程2-04 vue实例简单方法 vue实例简单方法: vm.$el -> 就是元素 vm.$data -> 就是data <!DOCTYPE html> <htm ...
- Vue 网络请求
Vue网络请求,用的是vue-resource 1. 首先需要安装vue-resource npm install vue-resource 2. 安装好之后,会在package.json文件中自动加 ...
随机推荐
- FusionCharts Free 甘特图
用FusionCharts做甘特图 1.同步方式(用xml格式字符) 前台aspx代码 <!DOCTYPE html> <html xmlns="http://www.w3 ...
- SDOI2017 Round1 Day2 题解
T2好厉害啊……AK不了啦……不过要是SCOI考这套题就好了240保底. BZOJ4819 新生舞会 模板题,分数规划+二分图最大权匹配. 费用流跑得过,可以不用KM. UPD:分数规划用迭代跑得飞快 ...
- [HNOI 2010] 弹飞绵羊
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2002 [算法] LCT动态维护森林连通性 时间复杂度 : O(NlogN ^ 2) ...
- C++可移植性和跨平台初探
概述 今天聊聊C++的可移植性问题.如果你平时使用C++进行开发,并且你对C++的可移植性问题不是非常清楚,那么我建议你看看这个系列.即使你目前没有跨平台开发的需要,了解可移植性方面的知识对你还是很有 ...
- MVC错误:查询的结果不能枚举多次
应用程序中的服务器错误. 查询的结果不能枚举多次. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: S ...
- CentOS6.6 zookeeper完全集群搭建
centos6.6搭建zookeeper-3.4.6完全分布式环境 转载 2015-06-28 22:14:17 标签:it 为了搭建HBase完全分布式环境,前提就是搭建好zookeeper和Had ...
- Android开发--Intent
一:Intent基础知识 Intent:信使,实现四大组件间的通信. 1:实现页面间的跳转,有两种方式: (1):startActivity()方式: (2) 2:使用Intent传递数据的四种方式: ...
- mongodb和mysql语法对比
MySQL: SELECT * FROM user Mongo: db.user.find() —————————————— MySQl: SELECT * FROM user WHERE name ...
- 11.ClientCredential模式总结
11.ClientCredential模式总结 服务端定义的Resource叫做api Resource API默认是被保护的 第三方的客户端先去请求 Server拿到access token.带着t ...
- VS Supercharger插件
一.前言 Supercharger 是 VS 的一款插件,针对代码进行优化和着色,便于观察和区分. 二.下载及安装 下载的 URL 如下:Supercharger 下载地址 点击下载,下载完成以后,那 ...