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状态绑定
猜你喜欢
- 1 Vue - 简介 2023-10-08
- jsPlumb+vue创建字段映射关系 2023-10-08
- 深入浅析AjaxFileUpload实现单个文件的 Ajax 文件上传库 2022-12-15
- vue keep-alive 2023-10-08
- ajax实现输入提示效果 2023-02-14
- 基于CORS实现WebApi Ajax 跨域请求解决方法 2023-02-14
- layui数据表格以及传数据方式 2022-12-13
- javascript 判断当前浏览器版本并判断ie版本 2023-08-08
- 关于 html:如何从 css 表中删除边距和填充 2022-09-21
- JS实现左侧菜单工具栏 2022-08-31