Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义layout组件,props里拿不到config的routes,没办法自己实现菜单的渲染? #10177

Closed
Johnnnnboy opened this issue Dec 29, 2022 · 5 comments

Comments

@Johnnnnboy
Copy link

Background

项目中需要多个layout布局

Proposal

项目中有多种layout的需求,那么就需要自定义layout组件,在layout组件内自己实现头部,菜单,底部。现在的问题在于layout组件的props是空的拿不到config的routes,这种情况下如何拿到routes并实现菜单的渲染呢?

Additional context

image

@github-actions
Copy link

由于缺乏足够的信息,我们暂时关闭了该 Issue。请修改(不要回复) Issue 提供最小重现以重新开启。谢谢。

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 29, 2022
@twinkle77
Copy link

umi3. 升级到 um4 会有这个问题,props 拿不到 route 信息了

我在看能不能通过 useAppData 来做

@SilentFlute
Copy link

SilentFlute commented Jan 28, 2023

可以通过useLocation来实现, umi4的文档中有这个api: useLocation, 详情可查看react-router的文档: react router_useLocation, 伪代码如下:

const Layout = () => {
  const location = useLocation();
  const { pathname } = location;
  
  if(pathname === '/custom-path') {
    return <Outlet />;
  }

  return <OtherComponents />;

@fz6m
Copy link
Contributor

fz6m commented Jan 28, 2023

useAppDatauseSelectedRoutes ,从这些里面可以获取到,详见文档。

@SilentFlute
Copy link

useAppDatauseSelectedRoutes ,从这些里面可以获取到,详见文档。

可以可以, 之前都没留意到, 也是个方法, 学到了

只是我试了下, useSelectedRoutes, 获取当前路径命中的所有路由信息, 获取到的结果永远包含最外层的/src/layout, 然后才到children, 也就是获取到的结果是父路由和子路由, 这是合理的, 只是对于想直接获取到子路由做判断的情形似乎有些冗余了, 不如获取当前的位置信息, 也就是location信息来得直观

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants