简体   繁体   English

在反应路由器路由中追踪正斜杠

[英]Trailing forward slash in react-router routes

I am using react-router v3.0.0 with react v15.1.0. 我正在使用react-router v3.0.0和react v15.1.0。 I have the following route setup: 我有以下路线设置:

ReactDom.render(<Provider store={store}>
    <Router history={BrowserHistory}>
        <Route path='shop' component={App}>
            <IndexRoute component={Shop} />
            <Route path='/product' component={ProductView} />
        </Route>
    </Router>
</Provider>, document.getElementById('app'));

As you can see, my base Route for the application has a path of 'shop' . 如您所见,我的应用程序的基本Route有一个'shop'的路径。 As far as the user is concerned, this should result in 2 separate routes, http://example.com/shop and http://example.com/shop/product . 就用户而言,这应该产生两条不同的路线, http://example.com/shophttp://example.com/shop/product However, this is not the case. 然而,这种情况并非如此。

When I deploy the aforementioned code, http://example.com/shop renders correctly, but http://example.com/shop/product renders nothing. 当我部署上述代码时, http://example.com/shop正确呈现,但http://example.com/shop/product呈现任何内容。 In fact, I get a console error: 事实上,我收到一个控制台错误:

Warning: [react-router] Location "/shop/product" did not match any routes

So, I altered my setup a bit: 所以,我改变了我的设置:

ReactDom.render(<Provider store={store}>
    <Router history={BrowserHistory}>
        <Route path='shop/' component={App}>
            <IndexRoute component={Shop} />
            <Route path='product' component={ProductView} />
        </Route>
    </Router>
</Provider>, document.getElementById('app'));

This will allow me to render http://example.com/shop/ (notice the trailing forward slash), http://example.com/shop/product , but NOT http://example.com/shop . 这将允许我渲染http://example.com/shop/ (注意尾随斜杠), http://example.com/shop/product但不是http://example.com/shop

Is is possible to render http://example.com/shop , http://example.com/shop/ , http://example.com/shop/product within the same app? 是有可能使http://example.com/shophttp://example.com/shop/http://example.com/shop/product同一应用程序内?

The reason that your first setup failed is because React Router <Route> path s that begin with a slash are considered absolute to the root ( / ), even when they are nested. 第一次安装失败的原因是因为以斜杠开头的React Router <Route> path被认为是根( / )的绝对path ,即使它们是嵌套的。

Your second setup is close, but you should not include the trailing slash in shop/ . 您的第二个设置已关闭,但您不应在shop/包含尾部斜杠。 React Router will join the paths together for you, you do not need to worry about including a slash to join shop and product . React Router将为您加入路径,您无需担心包含斜线来加入shopproduct (For example, look at this configuration which uses relative paths) (例如,查看使用相对路径的此配置

ReactDom.render(<Provider store={store}>
  <Router history={BrowserHistory}>
    <Route path='shop' component={App}>
        <IndexRoute component={Shop} />
        <Route path='product' component={ProductView} />
    </Route>
  </Router>
</Provider>, document.getElementById('app'));

You should use the route setup like 您应该使用路径设置

ReactDom.render(<Provider store={store}>
    <Router history={BrowserHistory}>
        <Route path='shop' component={App}>
            <IndexRoute component={Shop} />
            <Route path='/shop/product' component={ProductView} />
        </Route>
    </Router>
</Provider>, document.getElementById('app'));

You route didn't work because your product route was absolute with your url since it begins with a / . 你的路线没有用,因为你的product路线绝对是你的网址,因为它以/开头。 I think a better way would be to remove it and use the route as 我认为更好的方法是删除它并使用路由

<Route path='product' component={ProductView} />

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

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