简体   繁体   English

反应路由器不使用参数

[英]React router not working with params

I have the following setup:我有以下设置:

<div>
    <Router history={browserHistory}>
        <Route path="/" component={NewCustomerContainer} />
        <Route path="/newCustomer" component={NewCustomerContainer} />
        <Route path="/search" component={SearchPageContainer} />
        <Route path="/network" component={NetworkContainer} />
        <Route path="/network/:id" component={NetworkContainer} ></Route>
    </Router>
</div>

The route http://localhost:8100/network works fine.路由http://localhost:8100/network工作正常。 The route http://localhost:8100/network/abc123 does not with a 404 error appearing in my console.路由http://localhost:8100/network/abc123没有出现在我的控制台中的 404 错误。 Anyone seen anything like this before?有没有人见过这样的事情?

Something else to try is to add a其他尝试是添加一个

<base href="/">

tag inside the head tags of your base HTML page.标记在您的基本 HTML 页面的 head 标记内。

<div>
    <Router history={browserHistory}>
        <Route path="/" component={NewCustomerContainer} />
        <Route path="/newCustomer" component={NewCustomerContainer} />
        <Route path="/search" component={SearchPageContainer} />
        <Route path="/network" component={NetworkMetaContainer}>
            <Route path="/:id" component={NetworkContainer}/>
        </Route>
    </Router>
</div>

You forgot to nest the the '/id' inside the network Route.您忘记在网络路由中嵌套“/id”。 React Router allows for nested routing by placing Routes inside other Routes... If you have all network/id stuff inside the network and the network renders its children then this will work correctly. React Router 通过将 Routes 放置在其他 Routes 中来允许嵌套路由...如果您在网络中拥有所有 network/id 内容并且网络呈现其子代,那么这将正常工作。

The new NetworkMetaContainer will need to be built that has a simple render children... or if you want you can perhaps have a <IndexRoute /> inside the network Route, but either way the NetworkMetaContainer thing must be just the outer wrapper (maybe it'll have the different tabs or links or possibilities inside the Network)?新的 NetworkMetaContainer 将需要构建一个简单的渲染子<IndexRoute /> ......或者如果你愿意,你可以在网络路由内有一个<IndexRoute /> ,但无论哪种方式,NetworkMetaContainer 东西都必须只是外部包装器(也许它将有不同的选项卡或链接或网络内的可能性)?

try to add prop exact={true} inside Route component尝试在Route组件中添加 prop exact={true}

<div>
    <Router history={browserHistory}>
        <Route path="/" component={NewCustomerContainer} />
        <Route path="/newCustomer" component={NewCustomerContainer} />
        <Route path="/search" component={SearchPageContainer} />
        <Route path="/network" exact={true} component={NetworkContainer} />
        <Route path="/network/:id" exact={true} component={NetworkContainer} ></Route>
    </Router>
</div>

Asuming you are using react-router-dom https://reactrouter.com/web/guides/primary-components假设您使用的是 react-router-dom https://reactrouter.com/web/guides/primary-components

Put your more specific route before the other:将更具体的路线放在其他路线之前

<Route path="/posts/:id" exact>
  <Post />
</Route>
<Route path="/posts">
  <Posts />
</Route>

Note how these two routes are ordered.请注意这两条路线是如何排序的。 The more specific path="/contact/:id" comes before path="/contact" so that route will render when viewing an individual contact更具体的 path="/contact/:id" 出现在 path="/contact" 之前,以便在查看单个联系人时呈现路由

Base href didn't work for me. Base href 对我不起作用。 This did though:虽然这样做了:

<Router history={browserHistory}>
   <Route path={['/network/:id', '/network']} component={NetworkContainer} />
</Router>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM