gpt4 book ai didi

reactjs - 如何将所有路由重定向到 gatsby 索引

转载 作者:行者123 更新时间:2023-12-04 08:56:54 25 4
gpt4 key购买 nike

我正在尝试制作一个只有一页来处理所有路由的 Gatsby 项目。我有这样的索引页:

const App = () => {
return <Router>
<Home path="/"/>
<Login path="/login" />
<Content path="/content" />
</Router>
}
src/pages文件夹我只有 index.js我怎样才能让这个页面处理所有的路线?
检查 Gatsby 文档 ( https://www.gatsbyjs.com/docs/client-only-routes-and-user-authentication/ ) 我知道我可以使用插件 gatsby-plugin-create-client-paths像这样:
{
resolve: `gatsby-plugin-create-client-paths`,
options: { prefixes: [`/app/*`] },
},
如果我创建一个名为 app.js 的页面,这很有效。使用 react 路由器。所以所有路线 /app/*转到此页面。
但是我怎样才能在根 url 上进行这种重定向: / .我想做任何路线 /*进入索引页: index.js关于如何做到这一点的任何建议? :)

最佳答案

设置 index.js像这样:

const IndexPage = () => {
return (
<Router>
<Home path="/"/>
<Login path="/login" />
<Content path="/content" />
</Router>
)
}

export default IndexPage
并在 gatsby-node.js :
exports.onCreatePage = ({ page, actions }) => {
const { createPage } = actions
if (page.path === `/`) {
page.matchPath = `/*`
createPage(page)
}
}
重启 gatsby develop索引页面将处理您设置的路线。

关于reactjs - 如何将所有路由重定向到 gatsby 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63771163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com