javascript - 获取 Gatsby 中所有现有页面路由的数组
问题描述
我正在404
Gatsby 中创建一个页面,我想根据用户所访问的路径(不存在)为用户提供一个类似路径名的建议。如何获取网站中所有现有页面的数组?
解决方案
Gatsby 将您告诉它的每个页面的信息(通过pages
文件夹或createPage
API)公开为名为allSitePage
. 我倾向于在我的大多数项目中创建这样的钩子,因此很容易获得这些信息:
import { graphql, useStaticQuery } from "gatsby"
const useInternalPaths = () => {
const {
pages: { nodes },
} = useStaticQuery(graphql`
{
pages: allSitePage {
nodes {
path
}
}
}
`)
return nodes.map(node => node.path)
}
推荐阅读
- c# - 为什么更改 RichTextBox 控件的 TextChanged 事件中的文本不会导致堆栈溢出?
- json - PowerShell json重复键,将值压缩到单个数组
- c++ - Cgo 生成的源无法在 MVC 上编译
- android - Google Play 商店从不显示更新而不是打开
- python - How to log-in to site and Scrape with Python 3
- php - 根据 Woocommerce 中的选定值显示或隐藏注册字段
- mongodb - 将本地 mongo db 迁移到 atlas 后无法从 atlas 获取记录
- javascript - 在 Puppeteer 中获取所有带有 XPath 的链接(暂停或不工作)?
- c - 用函数排序数组而不使用 C 中的指针没有成功。为什么?
- reactjs - 从另一个组件获取值到 redux 表单字段中