• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小码哥-倩倩
博客园    首页    新随笔    联系   管理    订阅  订阅

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')就会执行一次,
posted @ 2019-07-09 10:19  小码哥-倩倩  阅读(341)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3