小编Vee*_*tap的帖子

我在 nextjs 中使用新的应用程序 Router 。获取错误:useRouter 仅适用于客户端组件。添加“use client”指令来使用它

./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

11
推荐指数
1
解决办法
1万
查看次数

标签 统计

next.js ×1