在NextJS中为特定页面创建不同的布局

huangapple go评论65阅读模式
英文:

Different Layout for certain pages in NextJS

问题

Here's the translated code portion without additional content:

我正在使用 Next.js我想在我的应用的每个页面上都有一个页眉和页脚除了登录页面我已经在应用目录中创建了一个 `layout.tsx` 文件用于将布局应用于所有页面这已经正常工作然而当我在登录目录中创建一个 `layout.tsx` 文件时页眉消失了但页脚仍然存在而且我会收到三个有关在初始加载时出现的错误的警告

我的登录页面如下所示

```jsx
import Image from 'next/image';
import LoginLayout from "./layout";
import { ReactNode } from 'react';

function Login() {
    const router = useRouter();

    function checkIfCredentialsAreValid() {
        router.push("/dashboard");
    }

    return (
        <main className="flex p-40 ms-8 justify-center place-items-center">
            <div>
                <Button color="dark" style={{ backgroundColor: "#333", color: "#fff" }} onClick={checkIfCredentialsAreValid}>
                    Anmelden
                </Button>
            </div>
        </main>
    );
}

Login.getLayout = function getLayout(page: ReactNode) {
    return <LoginLayout>{page}</LoginLayout>;
}

export default Login;

布局文件 layout.tsx 代码如下:

import '../globals.css';
import { Inter } from 'next/font/google';

const inter = Inter({ subsets: ['latin'] });

export const metadata = {
    title: 'Create Next App',
    description: 'Generated by create next app',
}

export default function Layout({
    children,
}: {
    children: React.ReactNode
}) {
    return (
        <html lang="en">
            <body className={inter.className}>
                {children}
            </body>
        </html>
    )
}

文件结构大致如下:

-app
 |-login
    |-layout.tsx
    |-page.tsx
  (其他页面按照相同的模式)
  |-global.css
  |-layout.tsx
  |-page.tsx

我希望这有助于您正确将布局应用于除登录页面之外的所有页面。

英文:

I'm using Next.js and I want to have a header and a footer on every page of my app, except for the Login page. I've already created a layout.tsx file in the app directory, which works fine for applying the layout to all pages. However, when I create a layout.tsx file inside the Login directory, the header disappears but the footer remains, and I get three warnings about errors while hydrating.

My LoginPage looks like this:

&#39;use client&#39;
import Image from &#39;next/image&#39;
import LoginLayout from &quot;./layout&quot;;
import { ReactNode } from &#39;react&#39;;

function Login() {
    const router = useRouter();

    function checkIfCredentialsAreValid() {


        router.push(&quot;/dashboard&quot;)

    }

    return (
        &lt;main className=&quot;flex p-40 ms-8 justify-center place-items-center&quot;&gt;
            &lt;div&gt;
                &lt;Button color=&quot;dark&quot; style={{ backgroundColor: &quot;#333&quot;, color: &quot;#fff&quot; }} onClick={checkIfCredentialsAreValid}&gt;
                    Anmelden
                &lt;/Button&gt;
            &lt;/div&gt;
        &lt;/main&gt;
    );
}

Login.getLayout = function getLayout(page: ReactNode) {
    return &lt;LoginLayout&gt;{page}&lt;/LoginLayout&gt;;
}

export default Login;

The code for my layout.tsx is this:

import &#39;../globals.css&#39;
import { Inter } from &#39;next/font/google&#39;


const inter = Inter({ subsets: [&#39;latin&#39;] })


export const metadata = {
  title: &#39;Create Next App&#39;,
  description: &#39;Generated by create next app&#39;,
}


export default function Layout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    &lt;html lang=&quot;en&quot;&gt;
      &lt;body className={inter.className}&gt;
        {children}
      &lt;/body&gt;
    &lt;/html &gt;
  )
}

And my File structure looks somewhat like this:

-app
 |-login
    |-layout.tsx
    |-page.tsx
  (other Pages after the same sceme)
  |-global.css
  |-layout.tsx
  |-page.tsx

I'd appreciate any help on how to properly apply the layout to all pages except for the Login page.

答案1

得分: 1

以下是您要翻译的内容:

For everyone coming around this Issue in the future:

我只是将 layout.tsx 重命名为类似 rootLayout.tsx,这样 nextJS 就会忽略它,然后通过 import 'RootLayout from '../rootLayout'; 将 RootLayout 导入到子页面,只有在需要时才实现 Root Layout。所以带有标题的页面的代码如下:

<RootLayout>
  <main>
    //内容
  </main>
</RootLayout>

如果您不想使用布局,只需不使用 RootLayout 标签

这样,每次重新加载页面或转到另一个子页面时,根布局都会被重新渲染。要防止这种情况,只需像这样导入 RootLayout:

import dynamic from 'next/dynamic';

const RootLayout = dynamic(
    () => import('../rootLayout'),
    { ssr: false }
);
英文:

For everyone coming around this Issue in the future:

I just renamed the layout.tsx to something like rootLayout.tsx so nextJS ignores it, then I importet the RootLayout to the sub pages via import RootLayout from &#39;../rootLayout&#39;; and just implemented the Root Layout when I needed it. So the Code for the Page with a Header looks like this:

&lt;RootLayout&gt;
  &lt;main&gt;
    //Content
  &lt;/main&gt;
&lt;/RootLayout&gt;

If you dont want the Layout, just dont use the RootLayout Tag

This way the Root Layout gets rerendered every Time you reload the Page or go to another SubPage. To prevent this, just import the RootLayout like this:

import dynamic from &#39;next/dynamic&#39;;

const RootLayout = dynamic(
    () =&gt; import(&#39;../rootLayout&#39;),
    { ssr: false }
);

huangapple
  • 本文由 发表于 2023年5月11日 15:30:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定