js 每5分钟刷新一次通过el-switch 来控制是否启用自动更新el-switch 通过 v-model 来绑定 store中的状态computed: {autoRefresh: {get() {return this.$store.state.dmx.mvAutoRefresh},set(value) {this.$store....
js 每5分钟刷新一次
通过el-switch 来控制是否启用自动更新
el-switch 通过 v-model 来绑定 store中的状态
computed: {
autoRefresh: {
get() {
return this.$store.state.dmx.mvAutoRefresh
},
set(value) {
this.$store.dispatch('dmx/setAutoRefresh', value)
}
},
dmxTimer() {
return this.$store.state.dmx.dmxTimer
}
}
切换至其他页面时,清除自动刷新,进入时 再根据 autoRefresh 来判断是否加上定时刷新
beforeDestroy() {
clearInterval(this.dmxTimer)
this.$store.dispatch('dmx/clearDmxTimer')
},
mounted() {
this.autoRefreshHandler()
},
methods: {
dmxtest() {
console.log((new Date()) + '----')
},
autoRefreshHandler() {
/** 定时器 **/
if (this.autoRefresh) {
if (this.dmxTimer == null) {
const tmpDmxTimer = setInterval(this.dmxtest, this.dmxTimeout)
this.$store.dispatch('dmx/setDmxTimer', tmpDmxTimer)
}
} else {
if (this.dmxTimer != null) {
clearInterval(this.dmxTimer)
this.$store.dispatch('dmx/clearDmxTimer')
}
}
}
}
dmx.js
// import Cookies from 'js-cookie'
const state = {
// 首页是否自动刷新
mvAutoRefresh: true,
message: 'xxx',
dmxTimer: null
}
const mutations = {
setAutoRefresh: (state, val) => {
state.mvAutoRefresh = val
},
updateMessage: (state, val) => {
state.message = val
},
clearDmxTimer: (state) => {
state.dmxTimer = null
},
setDmxTimer: (state, val) => {
state.dmxTimer = val
}
}
const actions = {
setAutoRefresh: (context, val) => {
context.commit('setAutoRefresh', val)
},
clearDmxTimer: (context) => {
context.commit('clearDmxTimer')
},
setDmxTimer: (context, val) => {
context.commit('setDmxTimer', val)
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
沃梦达教程
本文标题为:自动刷新实现,vuex状态绑定
基础教程推荐
猜你喜欢
- uniapp开发微信小程序自定义顶部导航栏功能实例 2022-10-21
- HTML clearfix清除浮动讲解 2022-11-20
- 使用ajax跨域调用springboot框架的api传输文件 2023-02-23
- 在实战中可能碰到的几种ajax请求方法详解 2023-02-01
- cocos creator游戏实现loading加载页面,游戏启动加载动画 2022-10-29
- Ajax发送和接收请求 2022-12-15
- Ajax+smarty技术实现无刷新分页 2022-12-15
- Javascript Bootstrap的网格系统,导航栏和轮播详解 2023-08-11
- JS滚动到顶部踩坑解决记录 2023-07-10
- 关于Ajax跨域问题及解决方案详析 2023-02-23