我正在使用带有react-router-dom的仪表板工作。我有一个结构,即“身份验证”页面和“仪表板”页面。当我从登录名导航到mainPages并尝试在DashboardContainer中选择一个导航链接时,向我显示白屏。
const MainContainer = ()=>{
return(
<BrowserRouter>
<div style={{flexDirection: 'row', display: 'flex'}}>
<Route path="/login" exact component={LoginPage}/>
<Route path="/main" exact component={DashBoardCotainer}/>
</div>
</BrowserRouter>
)
}
export default MainContainer;
单击登录按钮,然后将我重定向到仪表板
const LoginPage = (props)=>{
return(
<div style={{width: '100%', height: '100vh', alignItems: 'center', justifyContent: 'center', display: 'flex', flexDirection: 'column'}}>
<h1>Vendedor Solar</h1>
<button onClick={()=>{props.history.push( '/main' )}}>Logar</button>
</div>
)
}
export default LoginPage;
当我单击仪表板侧栏的导航链接时会发生问题
const DashBoardCotainer = () => {
return (
<div style={{flexDirection: 'row', display: 'flex'}}>
<Navigation/>
<div style={{padding: 20}}>
<Switch>
<Route path="/home" exact component={Home}/>
<Route path="/order" exact component={Orders}/>
<Route path="/contact" exact component={Contact}/>
<Route component={Error}/>
</Switch>
</div>
</div>
)
}
export default DashBoardCotainer;
class Navigation extends Component{
constructor(props){
super(props)
this.state={
navVisilbe:false
}
}
render(){
const {navVisilbe} = this.state
return(
<div style={{display:'flex', flexDirection: 'column', padding: 20, height: '100vh',
backgroundColor: mainOrange}}>
<NavLink to="/home" exact
>Home</NavLink>
<NavLink to="/order" exact
>Pedidos</NavLink>
<NavLink to="/contact" exact
>Contatos</NavLink>
</div>
)
}
}
export default Navigation
答案 0 :(得分:0)
您需要在调用dahboard组件的位置替换父组件中的所有路由。
const MainContainer = ()=>{
return(
<BrowserRouter>
<div style={{flexDirection: 'row', display: 'flex'}}>
<Route path="/login" exact component={LoginPage}/>
<Route path="/main" exact component={DashBoardCotainer}/>
<Switch>
<Route path="/home" exact component={Home}/>
<Route path="/order" exact component={Orders}/>
<Route path="/contact" exact component={Contact}/>
<Route component={Error}/>
</Switch>
</div>
</BrowserRouter>
)
}
export default MainContainer;
答案 1 :(得分:0)
所有这些嵌套的路由都是/main
,因此您需要适当地更新链接。
有关嵌套路线的更多详细信息:https://reacttraining.com/react-router/web/guides/quick-start/example-nested-routing
const MainContainer = () => {
return (
<BrowserRouter>
<div style={{ flexDirection: "row", display: "flex" }}>
<Route path="/login" exact component={LoginPage} />
<Route path="/main" component={DashBoardCotainer} />
</div>
</BrowserRouter>
);
};
const LoginPage = props => {
return (
<div
style={{
width: "100%",
height: "100vh",
alignItems: "center",
justifyContent: "center",
display: "flex",
flexDirection: "column"
}}
>
<h1>Vendedor Solar</h1>
<button
onClick={() => {
props.history.push("/main");
}}
>
Logar
</button>
</div>
);
};
const DashBoardCotainer = ({ match }) => {
return (
<div style={{ flexDirection: "row", display: "flex" }}>
<Navigation />
<div style={{ padding: 20 }}>
<Switch>
<Route path={`${match.url}/home`} exact component={() => "Home"} />
<Route path={`${match.url}/order`} exact component={() => "Orders"} />
<Route
path={`${match.url}/contact`}
exact
component={() => "Contact"}
/>
<Route component={() => "error"} />
</Switch>
</div>
</div>
);
};
class Navigation extends React.Component {
constructor(props) {
super(props);
this.state = {
navVisilbe: false
};
}
render() {
const { navVisilbe } = this.state;
return (
<div
style={{
display: "flex",
flexDirection: "column",
padding: 20,
height: "100vh"
}}
>
<NavLink to="/main/home" exact>
Home
</NavLink>
<NavLink to="/main/order" exact>
Pedidos
</NavLink>
<NavLink to="/main/contact" exact>
Contatos
</NavLink>
</div>
);
}
}
function App() {
return (
<div className="App">
<MainContainer />
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
答案 2 :(得分:0)
您应该为路线创建一个唯一的文件,您可以在其中创建“公共”和“私人”布局,例如
a
公共布局
class MainContainer extends React.Component {
render() {
return(
<BrowserRouter>
<Switch>
<PublicLayout path="/login" exact component={LoginPage} />
<PrivateLayout path="/home" exact component={Home} />
<PrivateLayout path="/order" exact component={Order} />
<PrivateLayout path="/contact" exact component={Contact} />
// remove the 'exact' from route and you can make the nested routing working perfectly.. below example
<PrivateLayout path="/contact/create" component={AddContact} />
</Switch>
</BrowserRouter>
)
}
}
export default MainContainer;
私人布局
export const PublicLayout = ({ component: Component, ...rest }) => {
return <Route {...rest} render={(props) => (
<React.Fragment>
{<Component {...props} />}
</React.Fragment>
)} />
};
您的导航侧边栏
export const PrivateLayout = ({ component: Component, ...rest }) => {
const token = localStorage.getItem("token");
return <Route {...rest} render={(props) => (
<React.Fragment>
<div className="sideBar"><Navigation /></div>
<div className="contentBar">
{!token ? <Redirect to={{ pathname: '/login', state: { from: props.location }}} /> : <Component {...props} /> }
</div>
</React.Fragment>
)} />
};
希望这会有所帮助:)