当前位置: 首页 > Web前端 > vue.js

Vue微前端父子实时通讯解决方案

时间:2023-03-31 19:30:05 vue.js

介绍我们知道微前端父子通讯的方式有很多,比如location、cookie、LocalStorage、window,但是它们都有一个共同的问题,那就是就是,他们不能实时监控变化,比如换肤或者多语言切换,如果要应用到子项目,必须刷新页面才能得到通知,改变原项目的原则为越少越好,很多vue项目都是基于vuex做全局数据共享的,所以就诞生了vuex-micro-frontends。特性父子传递,实时变化仅1kbinstallyarnaddvuex-micro-frontends#npminstallvuex-micro-frontendsuse//master主应用,负责发送数据importvuexMicro-frontendsfrom"vuex-micro-frontends";conststore=newVuex.Store({state:{name:"jack",age:10,gender:"men"},plugins:[vuexMicroFrontends.send()]//默认发送所有数据//plugins:[vuexMicroFrontends.send(['name','age'])]//只向子应用发送姓名和年龄数据});//slave,子应用,负责接收数据importvuexMicroFrontendsfrom"vuex-micro-frontends";conststore=newVuex.Store({state:{name:""},plugins:[vuexMicroFrontends.receive()]//默认接受父组件传递的所有数据//plugins:[vuexMicroFrontends.receive(['name'])],//只接受姓名字段数据});