javascript - 内容渲染长文本
问题描述
我正在弄清楚如何呈现标准长文本,但有问题。
请查看我的查询,该查询在 graphQL 操场上运行时有效。
{
allContentfulArticle {
nodes {
title
slug
para {
para
}
}
}
}
这是我的 article.js 文件
import React from "react"
import { graphql } from "gatsby"
const Article = props => (
<div>
<header>
<h1>{props.data.site.siteMetadata.title}</h1>
</header>
<main>
<h1>{props.data.contentfulArticle.title}</h1>
</main>
<footer>
<p>{props.data.site.siteMetadata.author}</p>
</footer>
</div>
)
export default Article
export const query = graphql`
query Article($id: String) {
site {
siteMetadata {
title
description
author
}
}
contentfulArticle(id: { eq: $id }) {
title
subtitle
slug
para {
para
}
}
}
`
奇怪的是,我可以title
轻松地呈现类似内容类型的内容 - 只是简短。
我可以看到我的title
,slug
和其他元数据字段显示在 localhost 上,但不是para
段落。
解决方案
尽管您的 GraphQL 查询看起来很完美,但您并没有呈现长文本字段。只需使用:
import React from "react"
import { graphql } from "gatsby"
const Article = props => (
<div>
<header>
<h1>{props.data.site.siteMetadata.title}</h1>
</header>
<main>
<h1>{props.data.contentfulArticle.title}</h1>
</main>
<p>Your long text is: {props.data.contentfulArticle.para.para}</p>
<footer>
<p>{props.data.site.siteMetadata.author}</p>
</footer>
</div>
)
export default Article
export const query = graphql`
query Article($id: String) {
site {
siteMetadata {
title
description
author
}
}
contentfulArticle(id: { eq: $id }) {
title
subtitle
slug
para {
para
}
}
}
`
推荐阅读
- python - KeyError:使用 Trepan3k 时出现“深蓝色”
- javascript - 如何在js文件中使用mapState?
- php - 如何在没有 .php 扩展名的情况下通过 php 脚本进行路由?
- android - Android 构建失败。添加firebase性能sdk后
- tensorflow - 如何配置 Tensorflow 以使用特定的 GPU?
- mocha.js - 如何在浏览器中运行很少的测试套件,在 Nodejs 环境 Mocha 中运行很少的测试套件?
- javascript - 将名称的键/值数组动态添加到对象数组中
- c++ - 如何在c ++中获取特定间隔内的随机数
- ruby-on-rails - application.rb 中 Kernel.spawn 和 rails 的奇怪行为
- apache-flink - 不平衡的 Flink 流式负载