【发布时间】:2020-03-31 01:01:23
【问题描述】:
非常感谢您
我正在尝试访问 Drawer 组件的子页面,但是当我尝试使用 hisotry.push ('route name') 时,它只是在 url 中替换它但不导航到链接,我通过历史使用导航服务我不知道干扰和组件是否是抽屉的孩子,但它具有相同的地址。
档案传奇
src/service/routes.js
import { createBrowserHistory } from 'history';
const history = createBrowserHistory();
export default history;
src/modules/sagas.js
function* detailsUsers({ payload: { user } }) {
yield put(defineInformationDetails(user));
history.push(`Drawer/EditAdm`);
}
src/router/index.js
import React from 'react';
import { Switch, Route } from 'react-router-dom';
import Login from '../pages/Login';
// import Panel from '../pages/Panel';
import Panel from './Drawer';
import * as isUserLogged from '../store/modules/auth/actions';
import ResetPassword from '../pages/ResetPassword';
import ChangePassword from '../pages/ChangePassword';
export default function Routes() {
return (
<Switch>
<Route path="/" exact component={Login} />
<Route path="/Login" component={Login} />
<Route path="/ResetPassword" component={ResetPassword} />
<Route path="/ChangePassword" component={ChangePassword} />
<Route path="/Drawer" component={Panel} />
</Switch>
);
}
src/routes/drawer.js
const [drawer, setDrawer] = useState({
name: 'Menu',
subname: '',
options: [
{
id: '1',
name: 'Home',
open: true,
link: `/`,
icon: () => <IconHome />,
route: {
path: `${path}/`,
exact: true,
main: () => <Home />,
},
suboptions: [],
},
{
id: '2',
name: 'Cadastro de Usuário',
open: false,
link: false,
icon: () => <IconUserList />,
route: null,
suboptions: [
{
id: '1',
name: 'Buscar/Editar Usuário',
open: false,
link: `/SearchUser`,
icon: () => <IconSearchFa />,
route: {
path: `${path}/SearchUser`,
exact: true,
main: () => <EditUser />,
},
},
{
id: '2',
name: 'Administrador',
open: false,
link: `/AdmRegistration`,
icon: () => <IconRegisters />,
route: {
path: `${path}/AdmRegistration`,
exact: true,
main: () => <AdmRegistrator />,
},
},
{
id: '5',
name: 'Fornecedor',
open: false,
link: `/SupplierRegistrator`,
icon: () => <IconRegisters />,
route: {
path: `${path}/SupplierRegistrator`,
exact: true,
main: () => <SuppliedRegistrator />,
},
},
{
id: '6',
name: 'Controle de acesso nos eventos',
open: false,
link: `/EventAcess`,
icon: () => <IconEvent />,
route: {
path: `${path}/EventAcess`,
exact: true,
main: () => <EventAcess />,
},
},
{
id: '7',
name: 'Editar Administrador',
open: false,
link: `/EditAdm`,
icon: () => {},
route: {
path: `${path}/EditAdm`,
exact: true,
main: () => <EditAdm />,
},
},
],
},
],
});
return (
<Router>
<AreaPanel>
<Header
name={
localStorage.getItem('NeoTicket@name') !== null
? localStorage
.getItem('NeoTicket@name')
.substring(0, 1)
.toUpperCase()
.concat(localStorage.getItem('NeoTicket@name').substring(1))
: 'Nome Usuário'
}
functionOnClickDrawer={() => setOpenDrawer(!openDrawer)}
openDrawer={openDrawer}
functionOnClickUser={() => setOpenUser(!openUser)}
openUser={openUser}
/>
<AreaDrawerPanel>
<AreaDrawer open={openDrawer}>
<Drawer
name={drawer.name}
subname={drawer.subname}
openDrawer={openDrawer}
options={drawerSearch.options}
functionOnClickOpenSuboption={id =>
functionOpenSuboption(id, drawerSearch)
}
functionSearchText={text => setSearchText(text)}
functionOnClickName={name => functionGetNamePage(name)}
path={path}
/>
</AreaDrawer>
<AreaContent open={openDrawer}>
<HeaderContainer>
{/* {drawer.options.map((option, index) => {
const { link } = option;
if (link) {
return (
<HeaderContent
key={index.toString()}
name={namePage === '' ? 'TESTE' : namePage}
/>
);
}
})} */}
<Switch>
{drawer.options.map((option, index) => {
const { link, route, suboptions } = option;
if (link) {
return (
<Route
key={index.toString()}
path={route.path}
exact={route.exact}
children={<route.main />}
/>
);
}
return suboptions.map((suboptions, indexParam) => {
return (
<Route
key={indexParam.toString()}
path={suboptions.route.path}
exact={suboptions.route.exact}
children={<suboptions.route.main />}
/>
);
});
})}
</Switch>
</HeaderContainer>
</AreaContent>
</AreaDrawerPanel>
</AreaPanel>
</Router>
我不明白怎么解决。
【问题讨论】:
-
当您在
Switch中开发路由组件时,您希望将最具体的路由放在列表顶部。 -
@Daniel 我不明白你的意思,可以写其他吗?
-
您想将
Route path="/"放在列表的最后,因为它是最不具体的路线。 -
@Daniel,可能是这样,但这对解决问题有什么帮助?
标签: reactjs react-router