【问题标题】:NextJS Dynamic Routes: How to capture index.tsxNextJS 动态路由:如何捕获 index.tsx
【发布时间】:2021-05-12 15:57:48
【问题描述】:

有没有办法从动态路由中捕获“/”url?

这是我尝试过的:

这给了我“http://localhost:3000/”上的错误 404,而不是进入 [...page] 组件。

【问题讨论】:

标签: next.js


【解决方案1】:

[...page].tsx 只会捕获 / 之后的路由,因此 http://localhost:3000/page1 或 http://localhost:3000/page1/page2 将进入 [...页].tsx

你可以从 nextjs 路由器中提取值

import {useRouter} from "next/router";

然后在你的组件中

const router = useRouter()

console.log(router.query.page)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-25
    • 2020-09-08
    • 2021-08-12
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2022-06-12
    相关资源
    最近更新 更多