- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 gatsby js 并试图弄清楚如何使用 Gatsby 链接创建页面级侧边栏,这些链接在同一页面的 div 内呈现新组件我可以使用react-router-dom 来做到这一点,但在 Gatsby 中我所能找到的就是如何创建博客文章,这让我发疯,因为我找到的每个教程都是相同的博客文章。
这是我的布局页面/layouts/index.js
export default ({ children }) => (
<div id="layout">
<header>
<h3>Header</h3>
<MainNav />
</header>
{children()}
</div>
)
关于页面/pages/about.js
export default ({ location, match }) => {
console.log('location = ', location, 'match = ', match );
return (
<div id="about">
<SideBar />
<div id="content">
// . add child template or component for link clicked in sidebar
</div>
</div>
);
};
我想做的是,当用户单击侧栏中的链接时,保留在 about 上,但根据在 about 侧栏中单击的 gatsby 链接呈现新组件或模板。
关于侧边栏组件/components/about/side-bar.js
const SideBar = () => {
return (
<div id="side-bar">
{/* <li><Link to='/about?sort=name'>work</Link></li> */}
<li><Link to="/about/work">work</Link></li>
<li><Link to='/about/hobbies'>hobbies</Link></li>
<li><Link to='/about/buildings'>buildings</Link></li>
</div>
)
}
上面的链接有问题,他们正在尝试转到名为的新页面。/关于/工作这不是我想做的。我再次尝试让它停留在大约位置,但在内容 div 内渲染一个新组件。
请帮助 Gatsby ,就文档而言,到处都是。好吧,也许只是我没有清楚地了解文档。谢谢
更新:我尝试添加一个使用 createPage 的页面,该页面对我有用,但它没有传递 match.params id
gatsby-node.js
exports.createPages = ({ boundActionCreators }) => {
const { createPage } = boundActionCreators;
const myComponent = path.resolve('src/pages/about/index.js');
createPage({
path: '/about/:id',
component: myComponent
})
}
最佳答案
经过很长时间的尝试了解 Gatsby ,我可以说我仍然不明白,因为它的文档非常庞大而且不是很清楚。但是一旦我开始查看节点 API 和 onCreatePage它给了我一些想法。这就是文档的字面意思。
onCreatePage
Called when a new page is created. This extension API is useful for programmatically manipulating pages created by other plugins e.g. if you want paths without trailing slashes.
所以这里唯一给我暗示的部分可能是帮助我的关键是这一行。 对于以编程方式操作其他人创建的页面很有用插件
无论如何,这至少让我编写了一些代码。大约 3 小时后,我发现一个插件正在做我想要用这个方法做的事情。该插件名为 gatsby-plugin-create-client-paths 这里的关键是客户端路径!!!!
这让生活变得有值(value)!所以在上面的例子中,我只是想能够使用 Gatsby 的路由器(它只是幕后的 React-router-dom ),将我和 id 或值传递给路由器 match.params 对象。它仍然没有,但它所做的是检查前缀后的任何路径,例如 /folder/ 在我的例子中 '/about/work 并使用模板组件重新创建页面(在我的例子中继续使用pages/about/index.js),这是我的模板。现在我们已经为/about/之后的所有链接渲染了 about/index.js ,然后我们可以使用一些内部 switch 语句来处理传递给/about/index.js 的位置。仍然没有得到 match.params 更新,但我确实得到了 props.location.pathname;这允许我提取前缀后的所有内容,以便在 switch 语句中使用,以根据路由路径名呈现我的特定组件。这里已经足够了,这里只是一个粗略的解决方案作为示例。
因此,将插件添加为 npm 安装。打开 gatsby.config.js 并将以下代码添加到导出中。
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-create-client-paths`,
options: { prefixes: [`/about/*`] },
},
]
}
然后在我的主要关于页面pages/about/index
import React from "react";
import SideBar from '../../components/about/side-nav';
export default (props) => {
const { pathname } = props.location;
var n = pathname.lastIndexOf('/');
var pageId = pathname.substring(n + 1);
const page = () => {
switch(pageId){
case '':
return (
<div>Work Page</div>
);
case 'work':
return (
<div>Work Page</div>
);
case 'hobbies':
return (
<div>Hobbies Page</div>
);
case 'buildings':
return (
<div>buildings Page</div>
);
}
}
return (
<div id="about">
<SideBar />
<div id="content">
{page()}
</div>
</div>
);
};
然后在我的侧边栏中我这样调用它。
<li><Link to="/about/work">work</Link></li>
<li><Link to='/about/hobbies'>hobbies</Link></li>
<li><Link to='/about/buildings'>buildings</Link></li>
希望这对其他人有帮助。毕竟,我开始真正质疑 Gatsby 的大部分内容,尤其是文档不是很清楚。根据对我的问题的回答,我想 stackoverflow 社区中没有多少人使用 Gatsby,当您需要帮助时,这会令人担忧。看起来 Gatsby 的 github 社区确实非常有帮助,但这应该是针对 bug 问题而不是像我这样的问题,但令人鼓舞。
希望这对某人有帮助。
关于reactjs - Gatsby 一页一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51911847/
我正在尝试将图像的路径作为 Prop 传递给组件 注意:组件和索引页都可以通过相同的路径访问图像。当我将路径作为 Prop 传递时它不起作用 在下面的代码中,我尝试使用 GatbsyImage 和 S
我在 gatsby 中的主要字体是在 index.scss 文件夹中定义的,就像这样在 body 标签中。 body { font-family: "Trebuchet MS", "Helveti
我一直在遵循以下步骤:https://github.com/strapi/gatsby-source-strapi 由于 image >> publicURL 也不起作用,我重新安装了最新版本的 ga
语境 Gatsby , headless CMS 奇迹,旁边是 gatsby-plugin-advanced-sitemap 使生成一个强大的 sitemap.xml文件轻而易举。 该插件支持通过 s
目前,我有两个使用 gatsby.js 开发的站点部署到 example.com 和 blog.example.com。我想创建一个 blog.example.com 的子目录到 example.co
我一直在学习本教程 https://www.gatsbyjs.org/packages/gatsby-image/#art-directing-multiple-images但普通人不可能写出 50
我正在使用 Gatsby 的 createResolver API 将 markdown 转换为 html。它在顶层数据上运行良好。但是,我无法让它在嵌套更深的数组上工作。 这是有效的: functi
当我使用 Gatsby (V2) 路由到新页面时,我正在尝试传递数据 我希望然后能够检索该页面上的数据。这可能吗?我已经研究了很多但没有运气所以我觉得我一定错过了什么......谢谢 最佳答案 如果你
默认情况下,我的 Gatsby 网址类似于 2018-09-06-hexagon 有什么办法可以让他们变成/blog/2018/09/06/hexagon ? 这是我的 gatsby-node.js
我有一个 json 文件和一个包含图像的文件夹 plugins/ └── gatsby-source-cars/ ├── images/ ├── cars.json └── g
在生产环境中运行 gatsby。 16. 在开发机器上工作。 来自服务器的错误: success write out redirect data - 0.002s success Build mani
当我想安装 gatsby starter 时,我在终端中遇到了这些错误。 任何人都知道如何解决它? [4/4] 🔨 Building fresh packages... [6/13] ⠁ shar
我正在创建一个名为“new-app”的项目。成功安装 gatsby 后,我尝试运行命令“gatsby new my-app”。我总是收到一个错误,其中指出: ERROR eating new site
我无法使用 gatsby cli 克隆 gatsby-starter-default.git,因为它使用的“git”url 被我们的防火墙规则阻止 我也尝试将以下内容添加到 git config 但仍
我正在创建一个名为“new-app”的项目。成功安装 gatsby 后,我尝试运行命令“gatsby new my-app”。我总是收到一个错误,其中指出: ERROR eating new site
我按照健全性文档创建了一个 internalLink 类型,并根据有关将 internalLinks 与 graphql api 一起使用的提示,我将其创建为单独的类型,如下所示: export de
我创建了由 Gatsby Cloud(个人试用计划)托管并使用 Contentful 的 Gatsby.js 网站。虽然我没有设置 Robot.txt,但生产站点自动在 HTTP header 处添加
最令人沮丧的部分是我之前有这个工作然后不知何故破坏了它,但我正在使用 gatsby-plugin-sharp 和 gatsby-plugin-image 将照片添加到我的主页并看到这个错误: Gats
我创建了由 Gatsby Cloud(个人试用计划)托管并使用 Contentful 的 Gatsby.js 网站。虽然我没有设置 Robot.txt,但生产站点自动在 HTTP header 处添加
最令人沮丧的部分是我之前有这个工作然后不知何故破坏了它,但我正在使用 gatsby-plugin-sharp 和 gatsby-plugin-image 将照片添加到我的主页并看到这个错误: Gats
我是一名优秀的程序员,十分优秀!