【发布时间】:2018-09-29 10:44:23
【问题描述】:
我是 React 新手,正在尝试使用嵌套路由创建布局。这是我的场景
- 当 URL 为 / 时显示登录
- 当 URL 为 /dashboard 时显示仪表板
- 当 URL 为 /dashboard/profile 时显示配置文件(这应该加载 在仪表板内容区域内)
在浏览器中访问 URL 时,登录页面和仪表板页面正在正确加载,但对于 /dashboard/profile,浏览器会转到空白页面,而不是在仪表板组件中加载它。
索引.js
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById('root'));
App.js
class App extends Component {
render() {
return (
<div>
{/* <Switch> */}
<Route exact path='/' component={SignIn}/>
<Route exact path='/dashboard' component={Dashboard}/>
{/* </Switch> */}
</div>
);
}
}
export default App;
仪表板.js
class Dashboard extends React.Component {
render() {
const { classes } = this.props;
return (
<React.Fragment>
<CssBaseline />
<div className={classes.root}>
<Header classes={classes} open={this.state.open} click={this.handleDrawerOpen} />
<Sidebar classes={classes} open={this.state.open} click={this.handleDrawerClose} />
<main className={classes.content}>
<div className={classes.appBarSpacer} />
*********I expect profile component to load here
but when I access the URL /dashboard/profile I get a new blank page*********
Route path="/dashboard/profile" exact component={Profile} />
</main>
</div>
</React.Fragment>
);
}
}
【问题讨论】:
-
如何访问 /dashboard/profile 网址?通过浏览器或链接?
-
是的浏览器也...并在其他组件的某处使用链接
-
尝试使用HashRouter代替BrowserRouter
-
我在浏览器中尝试了像这样localhost:3000/#/dashboard/profile 访问gin URL,但它仍然给我一个空白页面,而不是在仪表板组件中打开配置文件组件
标签: reactjs react-router