jd-opensource / jd-opensource/micro-app-demo
react作为子应用时,在入口文件中如何跳转路由
- Dominant language
- JavaScript
- Stars
- 270
- Forks
- 139
- PR merge metrics
- No merged PRs in 30d
Description
//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) => {
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);
}
};
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the main.tsx or index.tsx entry point and inspect the handleMicroData flow around eventCenterForAppSource, useNavigate, and useLocation. Trace the initial data and listener callbacks, then define done as routing from the received path without the reported hook errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100