Strapi 富文本内容无法在网页上正确显示

JJM*_*M50 5 reactjs strapi next.js

我目前使用 Strapi 作为我的 CMS,使用 NextJs/React 作为我的前端。我在我的 Strapi 中包含了 1 个字段作为内容丰富的字段。当我在 Strapi 本身中添加内容并查看预览时,它显示了带有所有间距和下划线等的正确输出。但是当我切换到网页视图时,它全部都是 1 个句子,没有任何样式。我画下划线的地方会显示<u>Sentence</u>在我的网页中。

这是我当前的代码:

export default function name({ posts }) {
  return (
    <>
      <div>
        <div>
          {posts.Title}
        </div>
      </div>
      <div>
        {posts.Content}
      </div>

      <Carousel />
    </>
  );
}

// Tell nextjs how many pages are there
export async function getStaticPaths() {
  const res = await fetch("http://localhost:1337/blogs");
  const posts = await res.json();

  const paths = posts.map((blog) => ({
    params: { id: blog.id.toString() },
  }));

  return {
    paths,
    fallback: false,
  };
}

// Get data for each individual page
export async function getStaticProps({ params }) {
  const { id } = params;

  const res = await fetch(`http://localhost:1337/blogs?id=${id}`);
  const data = await res.json();
  const posts = data[0];

  return {
    props: { posts },
  };
}
Run Code Online (Sandbox Code Playgroud)

网页预览:

在此输入图像描述

导入react-markdown时出现以下错误

在此输入图像描述

Pie*_*rre 5

这是因为您必须将 markdown 转换为 HTML。您可以在本教程中找到一个示例:https://strapi.io/blog/build-a-blog-with-next-react-js-strapi

首先,安装“react-markdown”:

npm install react-markdown
# or
yarn add react-markdown
Run Code Online (Sandbox Code Playgroud)

下面的代码应该可以工作:

npm install react-markdown
# or
yarn add react-markdown
Run Code Online (Sandbox Code Playgroud)

顺便说一下,要在 Strapi 中仅检索一个条目,您应该使用单条目 API 端点:https://strapi.io/documentation/developer-docs/latest/developer-resources/content-api/content-api.html #获取条目

  • 当我尝试这个时,我收到错误。我在帖子中包含了错误的屏幕截图 (2认同)

小智 5

自Pierre 的回答以来,react-markdown 的语法发生了一些变化。这是为寻找同一问题答案的人提供的最新信息。

<ReactMarkdown>Your content</ReactMarkdown>
Run Code Online (Sandbox Code Playgroud)

来源链接: https: //github.com/remarkjs/react-markdown