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

标签 javascript node.js reactjs express react-router

我正在尝试加载组件:http://localhost:3001/api/posts/success

我的路线树如下所示:


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>

正在导出的模块

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;

当我点击页面时,我只看到导航栏.. enter image description here

编辑:解决方案是,当我们有一个 Wildroute :city 时,它将阻止所有其他路线,请注意

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

最佳答案

编辑:解决方案是,当我们有一个 Wildroute :city 时,它将阻止所有其他路线,请注意

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

因此,对于狂野路线,请确保使其与众不同

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

关于javascript - React 路由器未在重定向上加载组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59555869/

相关文章:

javascript - 如何设置 maxJsonLength 属性?

javascript - 自定义大小调整工具在 Canvas 绘图中不起作用

javascript - window.print 不打印 IE 中的所有页面

reactjs - 使用 next.js 时 typescript 上的错误数据类型

javascript - React 16.3 中的上下文组件无效

javascript 警报终止脚本的其余部分

javascript - Node.js:模块安装在 "addRemoteTarBall"上永远挂起

javascript - 如何仅呈现更新后的统计数据 - websockets

node.js - MongoDB、Mongoose 和复合 _id

javascript - react : TypeError: Cannot read property 'productID' of undefined