Next.js的_app布局不是持久的,它在页面切换时重新加载。

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

Nextjs _app layout is not persistent, it loads new on page change

问题

我已经创建了一个 _app.js 文件:

const MyApp = ({ Component, pageProps }) => {

    const getLayout = Component.getLayout || ((page) => <Layout>{page}</Layout>)

    return getLayout(<Component {...pageProps} />)

}
export default MyApp

还有两个页面 about.jsworks.js,它们只返回一个空的 div

export default function Works(){
    return(
        <div></div>
    )
}

我的默认布局定义如下:

const Layout = ({ children }) => {
    return (
        <div>
            <Navbar/>
            <main>{children}</main>
        </div>
    );
};
export default Layout;

但是当我尝试从 /works 导航到 /about 时,无论如何它都加载了导航栏(位于我的导航栏中):

...
<div className={styles.item}><a href={'/about'}>about</a></div>
...

我正在使用 Next.js 13.2.3,但不知道为什么它不起作用 Next.js的_app布局不是持久的,它在页面切换时重新加载。

我已经尝试过删除每个页面的布局并使用所有页面共享的布局,但结果相同,所以我又回到了每个页面的布局,因为我希望默认页面没有导航栏,而其他页面应该有一个。

StackOverflow 上的其他解决方案没有帮助,所以我正在尝试找出我的代码有什么问题。

英文:

i've created a _app.js:

const MyApp = ({Component, pageProps})  =\&gt; {

    const getLayout = Component.getLayout || ((page) =&gt; &lt;Layout&gt;{page}&lt;/Layout&gt;)
    
    return getLayout(&lt;Component {...pageProps} /&gt;)

}
export default MyApp

two pages about.js and works.js which just return an empty div:

export default function Works(){
    return(
        &lt;div&gt;&lt;/div&gt;
    )
}

and my default layout is defined as the following:

const Layout = ({children}) =&gt; {
    return (
        &lt;div&gt;
            &lt;Navbar/&gt;
            &lt;main&gt;{children}&lt;/main&gt;
        &lt;/div&gt;
    );
};
export default Layout;

but whyever it also loads the Navbar when i try to navigate from /works to /about with (lies in my navbar):

...
&lt;div className={styles.item}&gt;&lt;a href={&#39;/about&#39;}&gt;about&lt;/a&gt;&lt;/div&gt;
...

I'm using next 13.2.3 and idk why it doesn't work Next.js的_app布局不是持久的,它在页面切换时重新加载。

I already tried to remove the per-page layout and go with a shared layout for all the pages, but it had the same outcome - so i went back to my per-page, which i need because i want the default page without Navbar and all the other pages should have one.

Other solutions on StackOverflow didnt help, so i'm trying to figure out whats wrong with my code.

答案1

得分: 1

我找到了解决方案。我之前在页面之间的过渡中使用了<a>组件,但我应该使用next/link中的<Link> Next.js的_app布局不是持久的,它在页面切换时重新加载。

<div className={styles.item}><Link href={'/about'}>about</Link></div>
英文:

I found the solution. I was using &lt;a&gt; Components for my transition between pages, but i'm supposed to use &lt;Link&gt; from next/link Next.js的_app布局不是持久的,它在页面切换时重新加载。

&lt;div className={styles.item}&gt;&lt;Link href={&#39;/about&#39;}&gt;about&lt;/Link&gt;&lt;/div&gt;

答案2

得分: 0

以下是您要翻译的代码部分:

在您的 _app.js 文件中粘贴以下代码:

import "@/styles/globals.css";
import MyLayout from "@/components/layouts/layout";
import Navbar from "@/components/Navbar/Navbar";
import Layout from "@/components/layouts/layout";

const MyApp = (props) => {
  const { Component, pageProps } = props;

  return (
    <Layout>
      <Component {...pageProps} />
    </Layout>
  );
}

export default MyApp;

在您的 layout.js 文件中粘贴以下代码:

import React from "react";
import Navbar from "../Navbar/Navbar";
import Head from "next/head";
import { useRouter } from 'next/router';

const Layout = ({ children }) => {
  const router = useRouter();

  return (
    <div>
      {router.asPath === '/' ? null : <Navbar/>}
      <main>{children}</main>
    </div>
  );
};

export default Layout;

这将在所有页面上呈现布局,除了 /about 页面。

PS:我不得不分别运行 npm i react-moment@1.1.3 来运行您的项目。

英文:

Copy below code to your _app.js file

import &quot;@/styles/globals.css&quot;;
import MyLayout from &quot;@/components/layouts/layout&quot;;
import Navbar from &quot;@/components/Navbar/Navbar&quot;;
import Layout from &quot;@/components/layouts/layout&quot;;


const MyApp = (props)  =&gt; {  
  const { Component, pageProps } = props;
  
    return (
      &lt;Layout&gt;
       &lt;Component {...pageProps} /&gt;
      &lt;/Layout&gt;
    );

}
export default MyApp

Copy below code to your layout.js

import React from &quot;react&quot;;
import Navbar from &quot;../Navbar/Navbar&quot;;
import Head from &quot;next/head&quot;;
import  {useRouter} from &#39;next/router&#39;

const Layout = ({children}) =&gt; {
    const router = useRouter();

    return (
        &lt;div&gt;
            {router.asPath === &#39;/&#39; ? null : &lt;Navbar/&gt;}
            &lt;main&gt;{children}&lt;/main&gt;
        &lt;/div&gt;
    );
};

export default Layout;

This will render layout on all pages except /about page

PS: I had to seprately do ❯ npm i react-moment@1.1.3 to run you project

huangapple
  • 本文由 发表于 2023年3月15日 18:39:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743565.html
匿名

发表评论

匿名网友

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

确定