组件结构

主页面准备 - 图1

mock 数据

  1. const currentUser = {
  2. username:'test1',
  3. avatar:'https://yudafeng.github.io/static/default.png',
  4. bio:'user info'
  5. }
  1. const article = {
  2. "slug": "1371cdf8",
  3. "title": "hello",
  4. "description": "hello desc",
  5. "body": "# hello",
  6. "createdAt": "2022-01-20T05:28:57.000Z",
  7. "updatedAt": "2022-01-20T05:28:57.000Z",
  8. "userEmail": "lisi@qq.com",
  9. "tags": [
  10. "html",
  11. "tag1"
  12. ],
  13. "author": {
  14. "username": "lisi",
  15. "avatar": null,
  16. "bio": null,
  17. "createdAt": "2022-01-12T10:08:43.000Z",
  18. "updatedAt": "2022-01-12T10:08:43.000Z"
  19. }
  20. }
  1. const comments = [
  2. {
  3. "id": 4,
  4. "body": "comment body 2 ",
  5. "createdAt": "2022-01-22T11:14:58.000Z",
  6. "updatedAt": "2022-01-22T11:14:58.000Z",
  7. "userEmail": "zhangsan@qq.com",
  8. "articleSlug": "39dad7ae",
  9. "user": {
  10. "username": "zhangsan",
  11. "bio": null,
  12. "avatar": null
  13. }
  14. },
  15. {
  16. "id": 5,
  17. "body": "comment body 3 ",
  18. "createdAt": "2022-01-22T11:15:02.000Z",
  19. "updatedAt": "2022-01-22T11:15:02.000Z",
  20. "userEmail": "zhangsan@qq.com",
  21. "articleSlug": "39dad7ae",
  22. "user": {
  23. "username": "zhangsan",
  24. "bio": null,
  25. "avatar": null
  26. }
  27. }
  28. ]

基础结构

  1. class Article extends PureComponent {
  2. //1 获取文章slug
  3. constructor(props){
  4. super(props)
  5. this.state={
  6. slug:props.match.params.slug
  7. }
  8. }
  9. componentDidMount(){
  10. //2 获取文章详情 & 获取文章评论
  11. }
  12. render() {
  13. // const {article,comments,currentUser} = this.props
  14. //文章不存在
  15. if(!article){
  16. return null
  17. }
  18. // markdown数据>解析>html
  19. const markdata = article.body
  20. console.log(markdata);
  21. const markhtml = marked.parse(markdata)
  22. console.log(markhtml);
  23. const markobj = {__html:markhtml}
  24. // 是否自己的文章
  25. // 前置:登录
  26. const isOwn = currentUser&&currentUser.username === article.author.username
  27. return (
  28. <div>
  29. {/* 头信息: 标题 / 作者/ (关注 喜欢) / (修改 删除) */}
  30. {/* 文章信息: 内容 / 标签/ */}
  31. {/* 文章评论 */}
  32. </div>
  33. )
  34. }
  35. }