【发布时间】:2019-04-17 19:05:35
【问题描述】:
在我想到的示例中,我在/projects 有一个索引页。如果我单击页面上的某个项目,则会弹出一个包含更多详细信息的模式。现在我希望我的用户能够在打开模式的页面上添加书签,在一个像 /projects/1 这样的合理 URL 上。
如果我使用this.props.history.push("projects/1") 更改 URL,除非我先定义路由,否则浏览器中不会显示任何内容。但实际上我根本不想要一条单独的路线,只是一种改变 URL 外观的方法,除了显示模式之外不呈现其他一些组件(所以这个问题与例如 Changing the URL in react-router v4 without using Redirect or Link 不同)。
有谁知道该怎么做,或者是否有可能?
FWIW,这是我在 App.js 中的路由:
<Router>
<div>
<NavBar/>
<Switch>
<Route exact path='/' render={(props) => <Home {...props} buttonVisible={true} />}/>
<Route exact path='/projects' render={(props) => <ProjectsContainer {...props} projects={this.props.projectList} />} />
<Route path='/projects/:projectId/result' component={ResultsContainer} />
<Route exact path='/projects/new' render={(props) => (<div><Home {...props} buttonVisible={false}/></div>)}/>
</Switch>
</div>
</Router>
【问题讨论】:
标签: reactjs react-router