所以基本上我在应用程序目录中有一个服务器组件,我想获取路径名。我尝试使用 window.location 来做到这一点,但它不起作用。我有什么办法可以做到这一点吗?
./app/layout.tsx
ReactServerComponentsError:
You have a Server Component that imports next/router. Use next/navigation instead.
Import trace:
./app/layout.tsx
Run Code Online (Sandbox Code Playgroud)
如果我删除 use useRouter 并使用 useNavigation 然后我得到以下内容
Error: (0 , next_navigation__WEBPACK_IMPORTED_MODULE_2__.useNavigation) is not a function
Source
app/layout.tsx (18:30) @ useNavigation
16 | children: React.ReactNode
17 | }) {
> 18 | const router = useNavigation()
| ^
19 |
20 | const excludeHeader = router.pathname.startsWith('/admin')
Run Code Online (Sandbox Code Playgroud)
如果我在文件顶部添加 use client ,我会收到以下错误
You are attempting to export "metadata" from a component marked with "use client", which is disallowed. …Run Code Online (Sandbox Code Playgroud) 我正在 Next.js 13 中使用实验性的 appDir,并且想要获取请求的 URL 路径。在进行重定向之前,我需要此信息来设置搜索参数。我想获取服务器端的请求路径。
有一个与此类似的问题。如何使用 Next 13 获取应用程序目录中的当前路径名? 然而,它涉及在客户端获取路径名。
我尝试查看布局选项,它只有子项和参数的道具。我将无法使用该信息重建 URL。我想知道服务器端的请求来自哪里。我怎样才能实现这个目标?
我在获取动态路由中的参数时遇到问题。我在阅读时不断收到错误:
类型错误:无法解构“router.query”的属性“themenID”,因为它未定义。
另外,我不能使用next/router但必须集成路由器next/navigation。但这个没有查询属性。我使用 Next.js 版本 13
该路径的名称如下:http://localhost:3000/MainThema/2。
应用程序/MainThema/[themenID]/page.js:
"use client";
import { useRouter } from "next/navigation";
import React from "react";
export default function MainThema() {
const router = useRouter();
console.log(router);
const { themenID } = router.query;
return <div>MainThema </div>;
}
Run Code Online (Sandbox Code Playgroud)