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文件中自动加 ...
随机推荐
- c#设置系统时间后不起作用
网上设置系统时间的代码很多,但是会出现设置后没有作用的问题 遇到以上问题可以按照如下办法解决 1.项目--属性--安全性--勾选启用ClickOne安全设置,如下图所示: 2.打开app.manife ...
- SSL handshake_decode_error
查看布署在Amazon服务器上的日志时,发现如下错误: generated SERVER ALERT: Fatal - Handshake Failure - handshake_decode_err ...
- Opencv— — Pinch Filter
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- openssl生成公钥私钥对 加解密
在计算机软件开发世界中,编程语言种类极多,数据在各种语言的表现形式可能有所差异,但数据本身的处理可能,或者说本质上是完全一样的:比如数据在某个算法中的运算过程是一样的.在这里,我以加密与解密来作为例子 ...
- POJ 3764 The xor-longest( 树上异或前缀和&字典树求最大异或)
In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edg ...
- ubuntu下的google拼音输入法(终结版)
声明:此文章是从我的51cto博客上搬至于此. Ubuntu下SCIM应该是最好的中文输入法了,它与搜狗差不多,下面介绍它的安装方法: 1)终端输入: sudo apt-get remove scim ...
- P2759 奇怪的函数
题目描述 使得 x^x 达到或超过 n 位数字的最小正整数 x 是多少? 输入输出格式 输入格式: 一个正整数 n 输出格式: 使得 x^x 达到 n 位数字的最小正整数 x 输入输出样例 输入样例# ...
- 微信小程序 实现websocket长连接 以及断开连接之后自动重连
app.js let socketMsgQueue = [] let isLoading = false App({ globalData: { userInfo: null, localSocket ...
- 【原】Cache Buffer Chain 第四篇
作者:david_zhang@sh [转载时请以超链接形式标明文章] 链接:http://www.cnblogs.com/david-zhang-index/p/3873357.html [测试1]低 ...
- view-source协议
转自;https://blog.csdn.net/yuwq123/article/details/79481829