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文件中自动加 ...
随机推荐
- Spring MVC 和 Struts2 的区别?
1.请求拦截级别 struts2框架是类级别的拦截,每次来了请求就创建一个Action,然后调用setter getter方法把request中的数据注入 struts2实际上是通过setter ge ...
- 在线判题系统hustoj的搭建
摘要:ACM/ICPC程序设计竞赛,越来越受到各个高校的重视,是程序设计竞赛中的奥林匹克.Hustoj是搭建在linux系统上的判题系统.能够判断代码的正确性.会及时返回通过或者不通过,如果不通过会返 ...
- 详细的解说public,protected,Default和private的权限问题
详细的解说public,protected,Default和private的权限问题 让人更好的了解public,protected,Default和private他们之间的权限问题,我会做一个直观的 ...
- git学习笔记(上)
1 安装 win安装 地址 安装之后自报家门 $ git config --global user.name "Your Name" $ git config --global u ...
- bootstrap 学习笔记(4)---- 按钮
平常我们自己写按钮,这次不用我们自己写 了,直接应用bootstrap中的按钮样式,就能设计出很漂亮的按钮样式.接下来就让我们一起学习吧. 1.可以作为按钮使用的标签或元素:<a>< ...
- [APIO 2010] 特别行动队
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1911 [算法] 设前i个士兵"修正"后的最大战斗力为fi 令su ...
- C++基础--完善Socket C/S ,实现客户端,服务器端断开重连
// WindowsSocketServer.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include <iostream> ...
- 三 vue学习三 从读懂一个Vue项目开始
源码地址: https://github.com/liufeiSAP/vue2-manage 我们的目录结构: 目录/文件 说明 build 项目构建(webpack)相关代码. config ...
- CS231n 2016 通关 第五、六章 Batch Normalization 作业
BN层在实际中应用广泛. 上一次总结了使得训练变得简单的方法,比如SGD+momentum RMSProp Adam,BN是另外的方法. cell 1 依旧是初始化设置 cell 2 读取cifar- ...
- ZipHelper
using ICSharpCode.SharpZipLib.Zip; using System.Collections.Generic; using System.IO; namespace WLYD ...