Comment评论

对网站内容的反馈、评价和讨论。

何时使用

评论组件可用于对事物的讨论,例如页面、博客文章、问题等等。

Comment评论 - 图1

基本评论

一个基本的评论组件,带有作者、头像、时间和操作。

  1. <template>
  2. <a-comment>
  3. <template slot="actions">
  4. <span>
  5. <a-tooltip title="Like">
  6. <a-icon
  7. type="like"
  8. :theme="action === 'liked' ? 'filled' : 'outlined'"
  9. @click="like"
  10. />
  11. </a-tooltip>
  12. <span style="padding-left: '8px';cursor: 'auto'">
  13. {{likes}}
  14. </span>
  15. </span>
  16. <span>
  17. <a-tooltip title="Dislike">
  18. <a-icon
  19. type="dislike"
  20. :theme="action === 'disliked' ? 'filled' : 'outlined'"
  21. @click="dislike"
  22. />
  23. </a-tooltip>
  24. <span style="padding-left: '8px';cursor: 'auto'">
  25. {{dislikes}}
  26. </span>
  27. </span>
  28. <span>Reply to</span>
  29. </template>
  30. <a slot="author">Han Solo</a>
  31. <a-avatar
  32. src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  33. alt="Han Solo"
  34. slot="avatar"
  35. />
  36. <p slot="content">We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.</p>
  37. <a-tooltip slot="datetime" :title="moment().format('YYYY-MM-DD HH:mm:ss')">
  38. <span>{{moment().fromNow()}}</span>
  39. </a-tooltip>
  40. </a-comment>
  41. </template>
  42. <script>
  43. import moment from 'moment'
  44. export default {
  45. data () {
  46. return {
  47. likes: 0,
  48. dislikes: 0,
  49. action: null,
  50. moment,
  51. }
  52. },
  53. methods: {
  54. like() {
  55. this.likes = 1
  56. this.dislikes = 0
  57. this.action = 'liked'
  58. },
  59. dislike() {
  60. this.likes = 0
  61. this.dislikes = 1
  62. this.action = 'disliked'
  63. }
  64. }
  65. }
  66. </script>

Comment评论 - 图2

配合 List 组件

配合 List 组件展现评论列表。

  1. <template>
  2. <a-list
  3. class="comment-list"
  4. :header="`${data.length} replies`"
  5. itemLayout="horizontal"
  6. :dataSource="data"
  7. >
  8. <a-list-item slot="renderItem" slot-scope="item, index">
  9. <a-comment
  10. :author="item.author"
  11. :avatar="item.avatar"
  12. >
  13. <template slot="actions">
  14. <span v-for="action in item.actions">{{action}}</span>
  15. </template>
  16. <p slot="content">{{item.content}}</p>
  17. <a-tooltip slot="datetime" :title="item.datetime.format('YYYY-MM-DD HH:mm:ss')">
  18. <span>{{item.datetime.fromNow()}}</span>
  19. </a-tooltip>
  20. </a-comment>
  21. </a-list-item>
  22. </a-list>
  23. </template>
  24. <script>
  25. import moment from 'moment'
  26. export default {
  27. data () {
  28. return {
  29. data: [
  30. {
  31. actions: ['Reply to'],
  32. author: 'Han Solo',
  33. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  34. content: 'We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
  35. datetime: moment().subtract(1, 'days'),
  36. },
  37. {
  38. actions: ['Reply to'],
  39. author: 'Han Solo',
  40. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  41. content: 'We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
  42. datetime: moment().subtract(2, 'days'),
  43. },
  44. ],
  45. moment,
  46. }
  47. },
  48. }
  49. </script>

Comment评论 - 图3

嵌套评论

评论可以嵌套。

  1. <template>
  2. <a-comment>
  3. <span slot="actions">Reply to</span>
  4. <a slot="author">Han Solo</a>
  5. <a-avatar
  6. slot="avatar"
  7. src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  8. alt="Han Solo"
  9. />
  10. <p slot="content">We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure).</p>
  11. <a-comment>
  12. <span slot="actions">Reply to</span>
  13. <a slot="author">Han Solo</a>
  14. <a-avatar
  15. slot="avatar"
  16. src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  17. alt="Han Solo"
  18. />
  19. <p slot="content">We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure).</p>
  20. <a-comment>
  21. <span slot="actions">Reply to</span>
  22. <a slot="author">Han Solo</a>
  23. <a-avatar
  24. slot="avatar"
  25. src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  26. alt="Han Solo"
  27. />
  28. <p slot="content">We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure).</p>
  29. </a-comment>
  30. <a-comment>
  31. <span slot="actions">Reply to</span>
  32. <a slot="author">Han Solo</a>
  33. <a-avatar
  34. slot="avatar"
  35. src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  36. alt="Han Solo"
  37. />
  38. <p slot="content">We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure).</p>
  39. </a-comment>
  40. </a-comment>
  41. </a-comment>
  42. </template>

Comment评论 - 图4

回复框

评论编辑器组件提供了相同样式的封装以支持自定义评论编辑器。

  1. <template>
  2. <div>
  3. <a-list
  4. v-if="comments.length"
  5. :dataSource="comments"
  6. :header="`${comments.length} ${comments.length > 1 ? 'replies' : 'reply'}`"
  7. itemLayout="horizontal"
  8. >
  9. <a-list-item slot="renderItem" slot-scope="item, index">
  10. <a-comment
  11. :author="item.author"
  12. :avatar="item.avatar"
  13. :content="item.content"
  14. :datetime="item.datetime"
  15. >
  16. </a-comment>
  17. </a-list-item>
  18. </a-list>
  19. <a-comment>
  20. <a-avatar
  21. slot="avatar"
  22. src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  23. alt="Han Solo"
  24. />
  25. <div slot="content">
  26. <a-form-item>
  27. <a-textarea :rows="4" @change="handleChange" :value="value" ></a-textarea>
  28. </a-form-item>
  29. <a-form-item>
  30. <a-button
  31. htmlType="submit"
  32. :loading="submitting"
  33. @click="handleSubmit"
  34. type="primary"
  35. >
  36. Add Comment
  37. </a-button>
  38. </a-form-item>
  39. </div>
  40. </a-comment>
  41. </div>
  42. </template>
  43. <script>
  44. import moment from 'moment'
  45. export default {
  46. data () {
  47. return {
  48. comments: [],
  49. submitting: false,
  50. value: '',
  51. moment,
  52. }
  53. },
  54. methods: {
  55. handleSubmit() {
  56. if (!this.value) {
  57. return;
  58. }
  59. this.submitting = true
  60. setTimeout(() => {
  61. this.submitting = false
  62. this.comments = [
  63. {
  64. author: 'Han Solo',
  65. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  66. content: this.value,
  67. datetime: moment().fromNow(),
  68. },
  69. ...this.comments,
  70. ]
  71. this.value = ''
  72. }, 1000)
  73. },
  74. handleChange(e) {
  75. this.value = e.target.value
  76. }
  77. },
  78. }
  79. </script>

API

PropertyDescriptionTypeDefault
actions在评论内容下面呈现的操作项列表Array|slot-
author要显示为注释作者的元素string|slot-
avatar要显示为评论头像的元素 - 通常是 antd Avatar 或者srcstring|slot-
content评论的主要内容string|slot-
datetime展示时间描述string|slot-