vue订阅模式--解决切换小区时多次刷新路由问题
遇到的问题:vue项目使用的是单页面实现方式,顶部有一个小区切换,每次切换之后数据需要重新请求一次,但是路由不变页面不刷新,所以只能通过刷新组件:
<sj-header class="shujudating" :show-community="showCommunity" @refreshRouter="refreshRouter"></sj-header>
<router-view :key="dataCenterRounter"/>
refreshRouter(){
if(this.showCommunity){
this.dataCenterRounter = new Date().getTime();
}
}
新建一个全局index.js文件
// 小区id改变后网络请求
let community = {
clientList: [],
listen (key, fn) {
this.clientList.push(fn) // 订阅的消息添加进缓存列表
},
trigger () {
let fns = this.clientList;
fns.forEach(fn => {
fn.apply(this)
})
}
}
Vue.prototype.$changeCommunity = community;
引用:
changCommunity(){
this.$changeCommunity.trigger();
},
mounted: function(){
this.$changeCommunity.listen(this.curCommunityPid,()=>{
this.$emit('refreshRouter')
});
}
this.curCommunityPid发生改变this.$emit('refreshRouter')就会执行一次,
浙公网安备 33010602011771号