Vue通过状态为页面切换添加loading、为ajax加载添加loading
以下方法需要引入vuex,另使用了vux的UI框架,ajax添加loading还引入了axios。
一、为页面切换添加loading。
loading.js:
import Vue from 'vue'
import Vuex from 'vuex'
import router from './router' Vue.use(Vuex) const store = new Vuex.Store({}) // 这里你可能已经有其他 module store.registerModule('myVux', { // 名字自己定义
state: {
isLoading: false
},
mutations: {
updateLoadingStatus (state, payload) {
state.isLoading = payload.isLoading
}
}
})
router.beforeEach(function (to, from, next) {
store.commit('updateLoadingStatus', {isLoading: true})
next()
}) router.afterEach(function (to) {
store.commit('updateLoadingStatus', {isLoading: false})
}) export default store;
main.js
import Vue from 'vue'
import Vuex from 'vuex'
import App from './App'
import router from './router'
import store from './loading' Vue.use(Vuex)
Vue.use(router) new Vue({
el: '#app',
router,
store,
template: '<App/>',
components: {
App
}
})
App.vue:
//在模板中加入以下vux组件
<loading :text="loadText" v-model="isLoading"></loading> import { Loading } from 'vux'
import { mapState } from 'vuex' export default {
name: 'app',
data(){
return {
loadText:"loading"
}
},
components: {
Loading
},
computed:{
...mapState({
isLoading: state => state.myVux.isLoading
})
}
}
二、为ajax添加loading(发送请求时显示,返回数据时消失)。
loading.js:
import Vue from 'vue'
import Vuex from 'vuex' import Axios from 'axios'
Vue.use(Vuex)
Axios.interceptors.request.use(config => {
console.log('ajax begin request')
store.commit('ajaxStar')
return config;
})
Axios.interceptors.response.use(config => {
console.log('ajax get response')
store.commit('ajaxEnd')
return config
})
store.registerModule('myVux', { // 名字自己定义
state: {
ajaxIsLoading: false
},
mutations: {
ajaxStar (state) {
state.ajaxIsLoading = true
},
ajaxEnd (state) {
state.ajaxIsLoading = false
}
},
getter : {
ajaxIsLoading: state => state.ajaxIsLoading
}
})
export default store;
main.js
import Vue from 'vue'
import Vuex from 'vuex'
import App from './App'
import router from './router'
import store from './loading' Vue.use(Vuex)
Vue.use(router) new Vue({
el: '#app',
router,
store,
template: '<App/>',
components: {
App
}
})
App.vue:
//在模板中加入以下vux组件
<loading :text="loadText" v-model="ajaxIsLoading"></loading> import { Loading } from 'vux'
import { mapState } from 'vuex' export default {
name: 'app',
data(){
return {
loadText:"loading"
}
},
components: {
Loading
},
computed:{
...mapState({
ajaxIsLoading: state => state.myVux.ajaxIsLoading }) } }
Vue通过状态为页面切换添加loading、为ajax加载添加loading的更多相关文章
- ajax加载菊花loading效果
Ajax异步请求的时候,一般都会利用一个动态的gif小图片来制作一个Ajax Loading,以便增加用户体验. 这里我们可以使用Spin.js,该js脚本压缩后5k,可以不用任何图片,任何外部CSS ...
- Entity Framework加载相关实体——延迟加载Lazy Loading、贪婪加载Eager Loading、显示加载Explicit Loading
Entity Framework提供了三种加载相关实体的方法:Lazy Loading,Eager Loading和Explicit Loading.首先我们先来看一下MSDN对三种加载实体方法的定义 ...
- JQuery+CSS3实现Ajax加载时loading效果
之前通过Ajax请求加载数据的时候,在数据还没有呈现出来前,为了更好的用户体验,总会弄个loading告诉用户其实内容正在加载,而不是网站崩了.但是貌似之前使用gif图片的情况比较多,可能是为了兼容各 ...
- fakeLoader页面加载前loading演示8种效果
提高用户体验的插件fakeLoader页面加载前loading演示8种效果 在线预览 下载地址 示例代码 <div id="main"> <div class=& ...
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法
博客分类: jquery-easyui jQueryAjax框架HTML 现象: AJAX返回的html无法做到自动渲染为EasyUI的样式.比如:class="easyui-layout ...
- 页面加载时loading效果
页面加载时loading效果: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- HTML页面处理以及资源文件的加载
Javascript 异步加载详解 这篇文章很详细的介绍了HTML的页面处理以及资源文件的加载. 本文总结一下浏览器在 javascript 的加载方式. 关键词:异步加载(async loading ...
- 《动手实现一个网页加载进度loading》
loading随处可见,比如一个app经常会有下拉刷新,上拉加载的功能,在刷新和加载的过程中为了让用户感知到 load 的过程,我们会使用一些过渡动画来表达.最常见的比如"转圈圈" ...
- jquery mobile 的loading提示“正在加载...”在不同版本中的不同实现方式
在jquery mobile开发中,在页面的切换.或者ajax获取数据时由于网速慢等其他原因,会有一个加载的时间,如果能在这段时间给一个“正在加载...”的提示,用户体验会更好.下面来简单的介绍一下在 ...
随机推荐
- CSS样式基础:
CSS:外部文件导入 <link rel="stylesheet" type="text/css" href="./style.css&quo ...
- Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings (容斥原理)
题目链接 Description Yaroslav thinks that two strings s and w, consisting of digits and having length n ...
- js常用util
/** 日期格式化 */Date.prototype.Format = function(format) { var o = { "M+" : this.getMonth() + ...
- 使用 jquery.webcam 进行asp.net 拍照
HTML 代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index. ...
- opencv使用findContours等方法出现内存损坏之类的不能调用问题
错误现象: 编译项目,如果该项目是debug,则在链接器输入配置opencv_world310d.lib如果是release则输入opencv_world310.lib,输入其中一个,多输,输错运行不 ...
- PCB生产企业自动化立体仓库/智能仓库库系统WMS/WCS解决方案
PCB生产企业自动化立体仓库/智能仓库库系统WMS/WCS解决方案 自动化立体仓库智能仓储系统WMS/WCS重要性调查 调查1(物流成本占总生产成本比例数据)1979年英国的第一次调查表明,在从原材料 ...
- github上虽然已经有实现好的Char RNN
前言 学习RNN的时候很多人应该都有看过Andrej Karpathy写的The Unreasonable Effectiveness of Recurrent Neural Networks,使用基 ...
- Ckeditor 4 复制粘贴截图并转化base64格式保存至数据库
虽然Ckeditor 中自带的有上传图片和文件的功能,但是有时候我们并不需要把图片保存至服务器的文件夹中. 反而是截图复制粘贴,把图片转化为base64格式保存在数据库中即可满足要求. 1.首先下载安 ...
- ELK(+Redis)-开源实时日志分析平台
################################################################################################### ...
- spring定时任务的集中实现
转载博主:感谢博主 http://gong1208.iteye.com/blog/1773177 Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前 ...