reactjs - Nextjs 中的路由器

标签 reactjs typescript debugging react-router next.js

我正在对使用 ReactJS+Typescript 构建的现有应用程序进行更改,并且我已将其与 Typescript 一起迁移到 NextJs。

如何更改路由器?我检查了 Nextjs 的文档,但我感到非常困惑。

目前路由位于我的 App.tsx 文件中。这是代码。我想进行更改,因为当我当时单击“handleSave”时,我的博客文章没有被保存。我认为问题出在路由上。

    return (
      <div className="All-Routes">
        <Switch>
          <Route
            exact
            path="/"
            component={props => <Index {...props} posts={this.state.posts} />}
          />
          <Route
            path="/posts/:id"
            component={props => (
              <Data {...props} post={this.state.posts[props.match.params.id]} />
            )}
          />
          <Route
            exactpath="/new"
            component={props => <Saving {...props} onSave={this.handleSave} />}
          />
        </Switch>
        <LocationDisplay />
      </div>
    );
  }
}
export default App;

最佳答案

创建一个routes.js文件,如下所示:

const nextRoutes = require('next-routes')
const routes = (module.exports = nextRoutes())

routes.add('/', 'index')

并使用expressjs编写一个server.js并添加以下行:

const routes = require('./routes')
const handler = routes.getRequestHandler(app)
...
server.get('*', (req, res) => {
    handler(req, res, parsedUrl)
  })

关于reactjs - Nextjs 中的路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60291130/

相关文章:

reactjs - 通过单击列标题对表格进行排序

javascript - React,想要获取当前元素的当前鼠标位置

javascript - 在 Angular 6 中处理来自 Google Charts 的选择事件(没有包装器,例如 ng2-google-charts!)

android - Karbonn A18 未在 Eclipse 中检测到以进行调试

Java 使用带引号的字符串常量

javascript - 异步/等待未按预期工作 : ReactJS+ Async Await

javascript - D3 + React Native - X 位置更改时数据点值不更新

visual-studio - 如何停止 visual studio 关于存在 'public' 修饰符的警告

Angular 拖放,事件中的空文件数组

javascript - Firebug:在断点期间,无法执行控制台语句