Is there a way to achieve outlets in next.js ? in regular react router i could use <outlet> to render child components

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

Is there a way to achieve outlets in next.js ? in regular react router i could use <outlet> to render child components

问题

Next.js 或类似的框架中是否有路由 Outlet 的实现?我想在同一页上使用子路由来渲染子组件,但我还没有找到适用于 Next.js 的相同功能。

英文:

Is there an implementation of router Outlet in next.js or similar ? I want to render child components on the same page using sub-routes, but I haven't been able to find anything for next.js that does the same.

答案1

得分: 1

由于您没有使用Next.js 13+的"app directory",因此没有原生的方式来处理模板/布局。相反,您必须创建自己的布局系统。

以下是一个示例:

// /components/Layout.js
export default function Layout({ children }) {
  return (
    <main>
      <header>My Website</header>
      <article>{children}</article>
      {/*  ^ 这是您的"<Outlet>" */}
    </main>
  )
}
// /pages/blog/post1.jsx
import Layout from "@components/Layout";

export default function BlogPost1() {
  return (
    <Layout>
      <h1>Blog post 1!</h1>
    </Layout>
  )
}
// /pages/blog/post2.jsx
import Layout from "@components/Layout";

export default function BlogPost2() {
  return (
    <Layout>
      <h1>Blog post 2!</h1>
    </Layout>
  )
}

不要字面上采用此示例,在实际情况中,您将使用动态路由器,例如[id].js

英文:

Since you're not using Next.js 13+'s "app directory" there is no native way of handling templates/layouts. Instead you have to create your own layout system

Here's an example:

// /components/Layout.js
export default function Layout() {
  return (
    &lt;main&gt;
        &lt;header&gt;My Website&lt;/header&gt;
        &lt;article&gt;{children}&lt;/article&gt;
                {/*  ^ this is your &quot;&lt;Outlet&gt;&quot; */}
    &lt;/main&gt;
  )
}
// /pages/blog/post1.jsx
import { Layout } from &quot;@components/Layout&quot;;

export default function BlogPost1() {
  return (
    &lt;Layout&gt;
        &lt;h1&gt;Blog post 1!&lt;/h1&gt;
    &lt;/Layout&gt;
  )
}
// /pages/blog/post2.jsx
import { Layout } from &quot;@components/Layout&quot;;

export default function BlogPost2() {
  return (
    &lt;Layout&gt;
        &lt;h1&gt;Blog post 2!&lt;/h1&gt;
    &lt;/Layout&gt;
  )
}

Don't take this example literally, in a real-world scenario you would use dynamic router like [id].js.

答案2

得分: 0

你可以使用类似这样的方式,

<b>app/layout.js</b>

// - 适用于所有路由
export default function RootLayout({ children }) {
  return (
    &lt;html&gt;
      &lt;body&gt;
        &lt;Header /&gt;
        {children}
        &lt;Footer /&gt;
      &lt;/body&gt;
    &lt;/html&gt;
  );
}

<b>app/dashboard/layout.js</b>

// - 适用于位于 app/dashboard/* 的路由片段
export default function DashboardLayout({ children }) {
  return (
    &lt;&gt;
      &lt;DashboardSidebar /&gt;
      {children}
    &lt;/&gt;
  );
}```

参考此链接,

[enter link description here][1][2]


  [1]: https://nextjs.org/blog/layouts-rfc
  [2]: https://www.youtube.com/watch?v=69-mnojSa0M

<details>
<summary>英文:</summary>

you can use something like this,

&lt;b&gt;app/layout.js&lt;/b&gt;

```// Root layout
// - Applies to all routes
export default function RootLayout({ children }) {
  return (
    &lt;html&gt;
      &lt;body&gt;
        &lt;Header /&gt;
        {children}
        &lt;Footer /&gt;
      &lt;/body&gt;
    &lt;/html&gt;
  );
}

<b>app/dashboard/layout.js</b>

// - Applies to route segments in app/dashboard/*
export default function DashboardLayout({ children }) {
  return (
    &lt;&gt;
      &lt;DashboardSidebar /&gt;
      {children}
    &lt;/&gt;
  );
}```

refer this,

[enter link description here][1][2]


  [1]: https://nextjs.org/blog/layouts-rfc
  [2]: https://www.youtube.com/watch?v=69-mnojSa0M

</details>



huangapple
  • 本文由 发表于 2023年7月23日 23:37:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749078.html
匿名

发表评论

匿名网友

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

确定