在 v5 中,我们可以?为可选参数添加尾随路由,但与 v6 一样,对相同内容的支持已被删除,那么编写以下代码的替代方法是什么?
<Route path="/cart/:id?" component={<CartPage />} />
需要声明包含字符串和参数的可选路径
现在我有了这个路径
<Route exact path="/tasks/:id" render={(props) => (
<AsyncTask {...props} profile={this.state.profile} />
)} />
Run Code Online (Sandbox Code Playgroud)
我需要
/tasks/:id/notification/:notificationId
Run Code Online (Sandbox Code Playgroud)
在哪里notification/:notificationId可选
我尝试以这种方式添加它,但它不起作用
/tasks/:id/(notification/:notificationId)?
Run Code Online (Sandbox Code Playgroud)
我需要知道我是否来自通知链接,将其标记为已读.
这很有效
/tasks/:id/(notification)?/:notificationId?
Run Code Online (Sandbox Code Playgroud)
但它没有匹配和路径 notificationId
假设我有以下两条路线:
...
<Route exact path="/:param1?/" component={Home}/>
<Route path="/news" component={News}/>
...
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试命中路由时/news,触发Home了参数的根路由param1...
我假设解决方案是在param1之前设置一个问号,/?param1这样它就可以从路由中保存,但我无法弄清楚如何在react-router v4中执行此操作
我对路线组件有点困惑.想象一下,我有两条路径有自己的路径:
<Route path='/person/add' exact component={PersonForm}/>
<Route path='/person/:id' exact component={PersonView}/>
Run Code Online (Sandbox Code Playgroud)
/ person/add应该显示一个表单,我可以在其中创建一个新的Person
/ person /:id应该显示具有给定id的人.
问题>>如果我导航到/ person/add它还会显示/ person /:id的组件,因为字符串"add"对":id"有效.
有没有办法可以避免这种情况?例如通过告诉:id应该是一个数字?
我正在尝试创建一个匹配以下所有网址的路由:
/product/foo
/product/foo/bar
Run Code Online (Sandbox Code Playgroud)
这是我目前的路线:
<Route path="/product/:productName(/:urlID)" handler={SomeHandler} />
Run Code Online (Sandbox Code Playgroud)
根据https://github.com/rackt/react-router/blob/master/docs/guides/basics/RouteMatching.md上的文档,此路由应完全匹配,但它与上述任一URL都不匹配.
我需要做些什么来支持这个可选参数?
我在React Router版本0.13.3上,如果我删除了(/:urlID)那么我可以匹配第一个URL而不是第二个URL.
我对如何从根执行(多个)可选路径参数有点困惑。我正在使用react-router 3和redux 4.3。
据我了解,(/:param1)(/:param2)应该可以工作,但加载应用程序时出现以下错误:
[react-router] 位置“/property/3633”与任何路由不匹配。
索引.js:
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { Router, browserHistory, Route } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
import configureStore from './store/configureStore';
import {MyContainer} from "./containers/MyContainer";
const store = configureStore();
const history = syncHistoryWithStore(browserHistory, store);
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<Route path="/(/:Type)(/:Id)" component={MyContainer}/>
</Router>
</Provider>,
document.getElementById('root'),
);
Run Code Online (Sandbox Code Playgroud)
仅供参考我已经尝试过:
path="(/:Type)(/:Id)"
path="(/:Type)/(/:Id)"
path="/(/:Type)/(/:Id)"
path="/(/:Type)(/:Id)"
path="/:Type/:Id" // Only works when params are …Run Code Online (Sandbox Code Playgroud) 我有一条路
<Route path="/account/:accountid/LoginPage"
component={LoginPage}/>
Run Code Online (Sandbox Code Playgroud)
如果网址是 - >,这可以正常工作/account/12332/LoginPage.我想要可选的查询参数.网址结构就像
/account/12322/LoginPage?User=A&User=B
Run Code Online (Sandbox Code Playgroud)
我修改了路径为
<Route path="/account/:accountid/LoginPage(/:User)"
component={LoginPage}/>
Run Code Online (Sandbox Code Playgroud)
在此之后,如果我尝试访问该网址,它不会将我重定向到相应的页面,而是会抛出错误
useBasename.js:56 Uncaught TypeError: history.getCurrentLocation is not a function
at Object.getCurrentLocation (useBasename.js:56)
at Object.getCurrentLocation (useQueries.js:64)
at Object.listen (createTransitionManager.js:246)
at Object.componentWillMount (Router.js:97)
at ReactCompositeComponent.js:347
at measureLifeCyclePerf (ReactCompositeComponent.js:75)
at ReactCompositeComponentWrapper.performInitialMount (ReactCompositeComponent.js:346)
at ReactCompositeComponentWrapper.mountComponent (ReactCompositeComponent.js:257)
at Object.mountComponent (ReactReconciler.js:45)
at ReactCompositeComponentWrapper.performInitialMount (ReactCompositeComponent.js:370)
Run Code Online (Sandbox Code Playgroud)