[英]Is it better to have a file for every article or reuse a single file for each article?
I'm developing a website in Next.js and a section of this website is dedicated to articles.我正在用 Next.js 开发一个网站,这个网站的一部分专门用于文章。 Each article has its page like for users profile.
每篇文章都有自己的页面,如用户个人资料。 But, for users profile I have a
[username].js
file where I fetch specific data from the database for each user and display it in a common style.但是,对于用户配置文件,我有一个
[username].js
文件,我从数据库中为每个用户获取特定数据并以通用样式显示它。
For the articles I wanted to do the same thing, but the content of an article can have bold text, links, and other HTML tags and when I fetch data from the database and paste it in the file in this way <div>{article.content}</div>
, the {article.content}
in the browser becomes for exemple I am playng <b>soccer</b> in this club: <a href="https://blablabla.com">Juventus</a>
, but the result that I want is "I am playing soccer in Juventus".对于文章,我想做同样的事情,但是文章的内容可以有粗体文本、链接和其他 HTML 标签,当我从数据库中获取数据并以这种方式将其粘贴到文件中时
<div>{article.content}</div>
,浏览器中的{article.content}
变成例如I am playng <b>soccer</b> in this club: <a href="https://blablabla.com">Juventus</a>
,但我想要的结果是“我在尤文图斯踢足球”。 How can I interpret HTML?我如何解释 HTML?
If I can't do it are there problems to have a folder that contains a file for each article (they could be 100+) ?如果我做不到,有一个文件夹包含每篇文章的文件(它们可能是 100 多篇)会不会有问题?
To be more clear this is [article].js
更清楚的是,这是
[article].js
import React, { useState } from "react";
import { useRouter } from "next/router";
import axios from "axios";
import baseUrl from "../../utils/baseUrl";
import { NoArticleFound } from "../../components/Tools/NoData";
import {
Header,
Image as UI_Image,
Divider,
Form,
Icon,
Button,
Comment,
} from "semantic-ui-react";
const Copenhagen = "../../public/Copenhagen.jpg";
import Image from "next/image";
function Article({ article, errorLoading }) {
if (errorLoading) return <NoArticleFound />;
const router = useRouter();
return (
<main>
<article>
<header>
<h1>{article.title}</h1>
</header>
<Header as="h3">
<UI_Image src={article.authorPicUrl} avatar />
<Header.Content>
{article.author}
<Header.Subheader>
{article.publicationDate} · {article.readingTime} min read
</Header.Subheader>
</Header.Content>
</Header>
<Divider />
{article.content}
<Divider hidden />
</article>
</main>
);
}
Article.getInitialProps = async (ctx) => {
try {
const { article } = ctx.query;
const title = article;
const res = await axios.get(`${baseUrl}/api/articles/${title}`);
return { article: res.data };
} catch (error) {
return { errorLoading: true };
}
};
export default Article;
Change your article.content
to as shown below.将您的
article.content
更改为如下所示。 This is how you can render HTML content.这就是您可以呈现 HTML 内容的方式。
<span
dangerouslySetInnerHTML={{
__html: article.content,
}}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.