micro-zoe / micro-app-demo

micro-app 案例
MIT License
220 stars 124 forks source link

react作为子应用时,在入口文件中如何跳转路由 #25

Open zhanglp520 opened 1 year ago

zhanglp520 commented 1 year ago

//main.tsx或者index.tsx中 const handleMicroData = () => { const navigate = useNavigate();//报错,原因是需要在组件中使用 const location = useLocation();//报错 if (window.eventCenterForAppSource) { const dt = window.eventCenterForAppSource.getData(); console.log("微应用【app-source】 getData:", dt); if (dt.path && typeof dt.path === "string") { console.log("子应用得到的路由-初始", dt.path, location.pathname); dt.path = dt.path.replace(/^#/, ""); if (dt.path && dt.path !== location.pathname) { debugger; navigate(dt.path as string); } } window.eventCenterForAppSource.addDataListener( (data: Record<string, unknown>) => { console.log("微应用【app-source】addDataListener:", data); if (data.path && typeof data.path === "string") { console.log("子应用得到的路由", data.path, location.pathname); data.path = data.path.replace(/^#/, ""); if (data.path && data.path !== location.pathname) { debugger; navigate(dt.path as string); } } } ); setTimeout(() => { window.eventCenterForAppSource.dispatch({ myname: "appSource" }); }, 3000); } };