场景:1.后台管理系统
2. 所有页面都用keep-alive进行了缓存
3. 当角色管理的数据变动时,需要同步更新用户管理的下拉框内容
解决办法: eventBus
声名一个全局委托对象
// main.js
Vue.prototype.$bus = new Vue();
更新列表后发送事件
此处用的是全局混入,可以参考AOP的使用,在列表新增,编辑或删除后触发
// mixin
delAfter() {
// 如果当前页没有声明busEvent,则默认“list”, 根据该字段可以在不同页面做相应处理
this.$bus.$emit("list-change", this.busEvent || "list");
},
接收事件
// userSetting.vue
async mounted() {
this.getList(true);
this.$bus.$on("list-change", (e) => {
if (e === "role") {
this.getRoleList();
}
});
},