是否可以使用 NextJS 对所有路由使用一次组件

Is it possible to use once a component for all routes with NextJS

我是 NextJS 的初学者
我的问题很简单:我只想知道是否可以使用 NextJS

// with react-router-dom
<Router>
    <>
        <Header />
        <Switch>
          <Route path="/">
                <Home />
            </Route>
            <Route path="/about">
                <About />
            </Route>
            <Route path="/users">
                <Users />
            </Route>
        </Switch>
    </>
</Router>

我希望在我的所有页面中都有 <Header />,而不是每次更改页面时都呈现

目前我在每个页面中调用我的header但是有重新初始化道具。

你不能也不应该,因为 NextJs 已经内置了切换模式。

参见相关 Migrating from React Router 部分:

With Next.js, you can express the same application structure in the file system. When a file is added to the pages directory it's automatically available as a route.

pages/about.js → /about
pages/blog.js → /blog
pages/index.js → /

对于 Next.js,您不使用 react-router-dom,因为 Next.js 有一个基于页面概念的基于文件系统的路由器。

无法将组件添加到路由中,您必须在每个索引页中添加它。

在此处查看他们的文档:https://nextjs.org/docs/routing/introduction