gpt4 book ai didi

javascript - 如何将样式从 Markdown 文件应用于 Gatsby 构建的网站的数组?

转载 作者:行者123 更新时间:2023-12-03 01:27:20 25 4
gpt4 key购买 nike

我正在使用多个模板来使用 Gatsby JS 创建网页,每个模板的所有信息都保存在 markdown 文件中(CSS 样式是通过 Bulma 完成的)。

----
path: /software/TestSoftware
title: TestSoftware
templateKey: mdSoftware
tags: ["analysis", "testing", "concrete"]
layoutType: page
---

每个页面都有不同数量的标签。我的问题是如何循环/迭代来设置 javascript 文件中每个标签的样式?我发现在 html 中包含脚本被认为是不好的形式(并不是说我的脚本当前正在工作)。关于使用 map 还是 forEach 是否合适的信息似乎也存在冲突。

这是我的示例组件(映射不起作用...):

class mdSoftwareInsetPage extends React.Component {
render() {
const {html, frontmatter} = this.props.data.markdownRemark;
var softwareTags = frontmatter.tags;
return (
<section className="section hero">
<div className="hero-body">
<div className="container has-text-centered">
<h1 className="title">
{frontmatter.title}
</h1>
</div>
</div>
</section>
<section className="section">
<div className="container content">
<h3 className="specialties">
Specialties
</h3>
<div className="tags">
<script>
softwareTags.map((item, i) =>
<span className="tag is-primary is-medium">
${item}
</span>)
</script>
</div>
</div>
</section>
)
}
}

呈现的网页对于包含“专业”的部分应具有以下 HTML:

<div className="container content">
<h3 className="specialties">
Specialties
</h3>
<div className="tags">
<span className="tag is-primary is-medium"> analysis </span>
<span className="tag is-primary is-medium"> testing </span>
<span className="tag is-primary is-medium"> concrete </span>
</div>
</div>

最佳答案

这应该有效:

<div className="tags">
{softwareTags.map(tag =>
<span className="tag is-primary is-medium" key={tag}>{tag}</span>
)}
</div>

Map 是首选,因为它返回一个数组(无需手动构建数组),并且数组是 React 中的有效子类型。

关于javascript - 如何将样式从 Markdown 文件应用于 Gatsby 构建的网站的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51469318/

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