相关疑难解决方法(0)

v6 中可选参数的替代方法

在 v5 中,我们可以?为可选参数添加尾随路由,但与 v6 一样,对相同内容的支持已被删除,那么编写以下代码的替代方法是什么?

<Route path="/cart/:id?" component={<CartPage />} />

react-router react-router-dom

42
推荐指数
2
解决办法
3万
查看次数

使用可选路径AND可选参数对路由器4进行反应

需要声明包含字符串和参数的可选路径
现在我有了这个路径

<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

reactjs react-router

11
推荐指数
1
解决办法
3106
查看次数

如何在react-router v4中的根路由上设置可选参数?

假设我有以下两条路线:

    ...
    <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中执行此操作

react-router react-router-v4

8
推荐指数
1
解决办法
4781
查看次数

按类型排除React Router中路径参数的值

我对路线组件有点困惑.想象一下,我有两条路径有自己的路径:

<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应该是一个数字?

javascript coercion reactjs react-router

8
推荐指数
1
解决办法
2769
查看次数

可选的React Router参数

我正在尝试创建一个匹配以下所有网址的路由:

/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

5
推荐指数
1
解决办法
7062
查看次数

根目录下的 React-router 可选路径参数

我对如何从根执行(多个)可选路径参数有点困惑。我正在使用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)

reactjs react-router react-router-redux

5
推荐指数
1
解决办法
1329
查看次数

如何使用React路由器设置带有可选查询参数的路由?

我有一条路

 <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)

query-parameters reactjs react-router

4
推荐指数
1
解决办法
8920
查看次数