简体   繁体   English

React路由器未在重定向上加载组件

[英]React router is not loading component on Redirect

I am trying to load a component on : http://localhost:3001/api/posts/success我正在尝试加载一个组件: http://localhost:3001/api/posts/success

my route tree looks like:我的路线树看起来像:


import SuccessMessage from './components/SuccessMessage';


<div>
      <Router>
        <Route component={Navbar} />
        <Switch>
            <Route exact path="/" component={Home} />
            <Route exact path="/api/posts/:city" component={ViewPost} />

            <Route exact path="/api/posts/item/:id" component={ItemDetails} />
                    <Route exact path="/api/posts/success" component={SuccessMessage} />
                    <Route exact path="/api/posts/termsofservice" component={termsOfService} />
            <Route exact path="/createpost" component={CreatePost} />

        </Switch>
      </Router>
    </div>

the module being exported正在导出的模块

import React from 'react';
import { Jumbotron, Button  } from 'react-bootstrap';

const SuccessMessage = () => {
    return (
        <Jumbotron>
            <h1>Post Successfully Submitted!</h1>
            <p>
                Your post will be reviewed to ensure it does not violate our Terms Of Service, upon approval it will be
                displayed on the main page. Edit or Delete your post in your User Settings.
            </p>
            <p>
                <Button variant="primary" onClick={() => (window.location = `http://localhost:3001/api/posts`)}>
                    >Go To Listings
                </Button>
                {'  '}
                <Button variant="primary" onClick={() => (window.location = `http://localhost:3001/api/posts/termsofservice`)}>
                    Read Our T.O.S
                </Button>
            </p>
        </Jumbotron>
    );
};

export default SuccessMessage;

when I hit the page I only see the navbar..当我点击页面时,我只看到导航栏.. 在此处输入图片说明

EDIT: The solution is that when we have a wildroute :city it will block all the other routes, notice编辑:解决方案是,当我们有一个wildroute :city 时,它将阻塞所有其他路由,注意

<Route exact path="/api/posts/:city" component={ViewPost} />

EDIT: The solution is that when we have a wildroute :city it will block all the other routes, notice编辑:解决方案是,当我们有一个wildroute :city 时,它将阻塞所有其他路由,注意

<Route exact path="/api/posts/:city" component={ViewPost} />

therefore, for wildroutes, ensure you make it distinct因此,对于wildroutes,确保你让它与众不同

<Route exact path="/api/posts/city/:city" component={ViewPost} />

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

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