根据要求的路线加载React SPA
我正在构建一个react-router-redux SPA。目前该应用程序运行良好。无论用户输入什么URL,服务器都会发送index.html并以根路由('/')开始。我想要实现的是,当用户输入URL时,服务器仍将发送index.html,但路由器将知道所请求的URL并从那里启动SPA。
例如,现在如果我输入URL mydomain/profiles/1,服务器将发送文件,我将被重定向到根路由。我希望当我输入相同的URL时,路由器将自动加载此路由的组件,而不是主组件。
这是我的路线:
<Switch>
<Route path='/logout' component={Logout} />
<Route path='/profile/person/:personId' component={PersonProfile} />
<Route path='/profile/sealant-customer/:customerId' component={SealantCustomerProfile} />
<Route path="/search/sealant-customers" component={SealantCustomerSearch} />
<Route path='/data/people' component={People} />
<Route path="/" exact component={Home} />
<Redirect to="/" />
</Switch>
它只能通过反应路由器链接,而不是通过浏览器地址栏。
这可能是一个明显的问题,我在论坛中找不到答案。
谢谢。
回答如下:不知道我的问题是否正确,但我在我的应用程序中使用了这个问题
class App extends React.Component {
render() {
return (
<Root>
<Router>
<Switch>
<Route path='/' component={Home} />
<Route path='/profiles/:profileId' component={Profile} />
</Switch>
</Router>
</Root>
)
}
通过路径发送到Route的profileId可以在Profile组件中提取为
props.match.params.profileId
根据要求的路线加载React SPA
我正在构建一个react-router-redux SPA。目前该应用程序运行良好。无论用户输入什么URL,服务器都会发送index.html并以根路由('/')开始。我想要实现的是,当用户输入URL时,服务器仍将发送index.html,但路由器将知道所请求的URL并从那里启动SPA。
例如,现在如果我输入URL mydomain/profiles/1,服务器将发送文件,我将被重定向到根路由。我希望当我输入相同的URL时,路由器将自动加载此路由的组件,而不是主组件。
这是我的路线:
<Switch>
<Route path='/logout' component={Logout} />
<Route path='/profile/person/:personId' component={PersonProfile} />
<Route path='/profile/sealant-customer/:customerId' component={SealantCustomerProfile} />
<Route path="/search/sealant-customers" component={SealantCustomerSearch} />
<Route path='/data/people' component={People} />
<Route path="/" exact component={Home} />
<Redirect to="/" />
</Switch>
它只能通过反应路由器链接,而不是通过浏览器地址栏。
这可能是一个明显的问题,我在论坛中找不到答案。
谢谢。
回答如下:不知道我的问题是否正确,但我在我的应用程序中使用了这个问题
class App extends React.Component {
render() {
return (
<Root>
<Router>
<Switch>
<Route path='/' component={Home} />
<Route path='/profiles/:profileId' component={Profile} />
</Switch>
</Router>
</Root>
)
}
通过路径发送到Route的profileId可以在Profile组件中提取为
props.match.params.profileId