有没有办法防止rootlayout被包裹在dashboardlayout周围?Next.js v13文档:

enter image description here

我的文件 struct :

enter image description here

我可以使用route groups;但是,这样做将禁用我的contact,pricing路由中的包装.有没有办法防止这种情况发生?我想在联系和定价页面上的主页导航栏,但不想在我的仪表板主页导航栏.

我try 使用路由组;然而,它禁用了我的定价和联系路由中的包装.

navbar.tsx

"use client";
import Link from "next/link";
import Image from "next/image";
import { useEffect, useState } from "react";
import { Disclosure } from "@headlessui/react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faBars, faTimes } from "@fortawesome/free-solid-svg-icons";

function joinClassName(...classes: string[]) {
  return classes.filter(Boolean).join(" ");
}

const navigationList = [
  {
    title: "API",
    href: "/api",
  },
  { title: "Pricing", href: "/pricing" },

  { title: "Contact", href: "/contact" },
  { title: "Demo", href: "/demo" },
];

export default function Navbar() {
  const [navbar, setNavbar] = useState(false);

  const changeBackground = () => {
    if (window.scrollY >= 64) {
      setNavbar(true);
    } else {
      setNavbar(false);
    }
  };

  useEffect(() => {
    changeBackground();
    window.addEventListener("scroll", changeBackground);
  });

  return (
  <Disclosure as="nav">
      {({ open, close }) => (
        <>
          <div
            className={joinClassName(
              navbar || open ? "dark:bg-black bg-white" : "bg-transparent",
              " fixed top-0 left-0 right-0 z-50 "
            )}
          >
           
          </div>
        </>
      )}
    </Disclosure>
  );
}

推荐答案

由于您的NavbarClient Component,您可以避免使用Route Groups,但可以通过使用usePathname来阻止它在/dashboard中显示,如下所示:

"use client";

// Other imports ...

import { usePathname } from "next/navigation";

export default function Navbar() {
  const pathname = usePathname();
  // useEffect and other codes ...

  if (pathname == "/dashboard") {
    return <></>;
  }
  return <div>Actual content</div>;
}

Javascript相关问答推荐

如何将元素排列得彼此靠近?

promise .all()永不解决

Klaro与Angular的集成

jQuery提交按钮重新加载页面,即使在WordPress中使用preventDefault()

React 17与React 18中的不同setState行为

使用JavaScript重新排序行

Exceljs:我们在file.xlsx(...)&#中发现了一个问题'"" 39人;

无法访问Vue 3深度监视器中对象数组的特定对象值'

这个值总是返回未定义的-Reaction

Eval vs函数()返回语义

基于props 类型的不同props ,根据来自接口的值扩展类型

如何在Node.js中排除导出的JS文件

面对代码中的错误作为前端与后端的集成

如何限制显示在分页中的可见页面的数量

在查看网页时,如何使HTML中的按钮工作方式类似于鼠标上的滚轮或箭头键?

背景动画让网站摇摇欲坠

使用线性插值法旋转直线以查看鼠标会导致 skip

如何组合Multer上传?

如何根据输入数量正确显示alert ?

JWT Cookie安全性