我正在设计react的侧面导航,当我单击它的项目时,我想在Sidenav的左侧部分显示相关组件。但是,React Router会更改URL,但不会在此部分中显示组件。这是我的代码:
Sidenav.js
import{ BrowserRouter,Route, Link } from 'react-router-dom'
import {routes} from './../../routes.js'
...
class Sidebar extends Component {
state = {
navActive : '0'
}
render () {
return (
<BrowserRouter>
<NavPanel dark style={{ backgroundColor: '#2d2e2e', height: '100vh', float:'right'}} >
<NavTitle style={{ fontFamily: 'IranSans', textAlign: 'Center' }}>
لوگو اینجا قرار بگیرد
</NavTitle>
<NavSection>
<NavSectionTitle />
<NavSectionTitle />
<NavLink key='1' style={linkStyles.base} rightEl={<FiMonitor style={linkStyles.Icon} />} className={this.state.navActive === '1' ? 'active' :' ' }
onClick={() => this.setState({ navActive:'1' })} style={this.state.navActive !== '1' ? {fontFamily:'IranSans'} : {...linkStyles.base, borderStyle:'solid',
borderWidth:'0px 5px 0px 0px',
borderColor:'#50d48b'
}
}
>
<Link style={{color:'lightblue'}} to='./../../Views/Contents/Dashboard.js'>داشبورد</Link>
</NavLink>
<NavLink key='2' style={linkStyles.base} rightEl={<IoIosAdd style={linkStyles.AddIcon} />} className={this.state.navActive === '2' ? 'active' :' ' }
onClick={() => this.setState({ navActive:'2' })} style={this.state.navActive !== '2' ? {fontFamily:'IranSans'} : {...linkStyles.base, borderStyle:'solid',
borderWidth:'0px 5px 0px 0px',
borderColor:'#50d48b'
}
}
>
<Link style={{color:'lightblue'}} to='./../../Views/Forms/CreateJob.js'>اضافه کردن فرصت شغلی</Link>
</NavLink>
....
</NavSection>
{routes.map((route, index) => (
<Route
key={index}
path={route.path}
exact={route.exact}
component={route.main}
/>
))}
</NavPanel>
</BrowserRouter>
)
}
}
export default Radium(Sidebar)
routes.js
import React from 'react'
export const routes = [
{
path: './../../Views/Contents/Dashboard.js',
exact: true,
main: () => <h2>y</h2>
},
{
path: './../../Views/Contents/CreateJob.js',
exact: true,
main: () => <h2>s</h2>
},
{
path: './../../Views/Contents/Job.js',
exact: true,
main: () => <h2>r</h2>
},
......
]
export default routes
HRPanel.js
import SideBar from './../../Components/SideBar/SideBar'
import NavBar from './../Navigation Bar/NavBar.js'
class HRPanel extends Component {
render () {
return (
<div id='App'>
<SideBar />
<NavBar />
</div>
)
}
}
export default HRPanel
App.js
import React from 'react'
import Login from './Components/Login/Login.js'
import HRPanel from './Components/HR Panel/HRPanel.js'
import CreateJob from './Views/Forms/CreateJob.js'
import BasicInfo from './Views/Contents/BasicInfo.js'
import Dashboard from './Views/Contents/Dashboard.js'
import Education from './Views/Contents/Education.js'
import Feedback from './Views/Contents/Feedback.js'
import Finance from './Views/Contents/Finance.js'
import Insurance from './Views/Contents/Insurance.js'
import Jobs from './Views/Contents/Jobs.js'
import Management from './Views/Contents/Management.js'
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
function App () {
return (
<Router>
<Switch>
<Route exact path='/' component={Login} />
<Route exact path='./Components/HR Panel/HRPanel.js' component={HRPanel} />
<Route exact path='./Views/Forms/CreateJob.js' component={CreateJob} />
<Route exact path='./Views/Contents/BasicInfo.js' component={BasicInfo} />
<Route exact path='./Views/Contents/Dashboard.js' component={Dashboard} />
<Route exact path='./Views/Contents/Education.js' component={Education} />
<Route exact path='./Views/Contents/Feedback.js' component={Feedback} />
<Route exact path='./Views/Contents/Finance.js' component={Finance} />
<Route exact path='./Views/Contents/Insurance.js' component={Insurance} />
<Route exact path='./Views/Contents/Jobs.js' component={Jobs} />
<Route exact path='./Views/Contents/Management.js' component={Management} />
</Switch>
</Router>
// <Login> </Login>
// <HRPanel> </HRPanel>
)
}
export default App
似乎是什么问题?
答案 0 :(得分:2)
您为Route
组件指定了path属性。 path
不是React组件文件的相对文件路径,它是要匹配的URL路径。将组件/文件所有位置的path
更改为要匹配的URL路径,而不是组件的路径。例如,如果您希望在用户导航到HRPanel
时显示/hr
/将path
的{{1}}更改为Route
。 <Route exact path='/hr' component={HRPanel} />
可能类似于:
App.js
同样, <Switch>
<Route exact path='/' component={Login} />
<Route exact path='/hr' component={HRPanel} />
<Route exact path='/jobs/create' component={CreateJob} /> />
// ... remaining paths
</Switch>
的道具to不正确。 Link
应该指定要导航到的URL路径,而不是组件的相对路径。看起来应该像这样:
to
希望有帮助