Skip to content

Commit

Permalink
成功展示一篇文章
Browse files Browse the repository at this point in the history
  • Loading branch information
slTrust committed Feb 18, 2021
1 parent 1ca504f commit 1943c64
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
10 changes: 9 additions & 1 deletion pages/index.tsx
Expand Up @@ -3,6 +3,7 @@ import {GetServerSideProps, NextPage} from "next";
import {getDatabaseConnection} from '../lib/getDatabaseConnection';
console.log('执行了 index.tsx')
import {Post} from '../src/entity/Post';
import Link from 'next/link';

type Props = {
posts: Post[];
Expand All @@ -13,7 +14,14 @@ const index: NextPage<Props> = (props) => {
return (
<>
<div>
{posts.map(post => <div key={post.id}>{post.title}</div>)}
<h1>文章列表</h1>
{posts.map(post =>
<Link key={post.id} href={`/posts/${post.id}`}>
<a>
{post.title}
</a>
</Link>
)}
</div>
</>
)
Expand Down
59 changes: 25 additions & 34 deletions pages/posts/[id].tsx
@@ -1,39 +1,30 @@
import { getPost, getPostIds, Post } from "lib/posts";
import { NextPage } from "next";
import React from 'react';
import {GetServerSideProps, NextPage} from 'next';
import {getDatabaseConnection} from '../../lib/getDatabaseConnection';
import {Post} from '../../src/entity/Post';

type Props = {
post: Post
post: Post
}
const postShow: NextPage<Props> = (props) => {
const { post } = props
return (
<div>
文章详情
<h1>{post.title}</h1>
<article dangerouslySetInnerHTML={{ __html: post.htmlContent }}>
</article>
const postsShow: NextPage<Props> = (props) => {
const {post} = props;
return (
<div>
<h1>{post.title}</h1>
<article dangerouslySetInnerHTML={{__html: post.content}}>
</article>
</div>
);
};

</div>
)
}

export default postShow;

export const getStaticPaths = async () => {
const idList = await getPostIds();
console.log(idList)
return {
paths: idList.map(id => ({ params: { id: id } })),
fallback: false
}
}
export default postsShow;

export const getStaticProps = async (x: any) => {
const id = x.params.id;
const post = await getPost(id);
return {
props: {
post: JSON.parse(JSON.stringify(post))
}
}
}
export const getServerSideProps: GetServerSideProps<any, { id: string }> = async (context) => {
const connection = await getDatabaseConnection();
const post = await connection.manager.findOne(Post, context.params.id);
return {
props: {
post: JSON.parse(JSON.stringify(post))
}
};
};

0 comments on commit 1943c64

Please sign in to comment.