Comment评论 - 图1

Comment评论

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

何时使用

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

Comment评论 - 图2

基本评论

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

  1. <template>
  2. <a-comment>
  3. <template slot="actions">
  4. <span key="comment-basic-like">
  5. <a-tooltip title="Like">
  6. <a-icon type="like" :theme="action === 'liked' ? 'filled' : 'outlined'" @click="like" />
  7. </a-tooltip>
  8. <span style="padding-left: '8px';cursor: 'auto'">
  9. {{ likes }}
  10. </span>
  11. </span>
  12. <span key="comment-basic-dislike">
  13. <a-tooltip title="Dislike">
  14. <a-icon
  15. type="dislike"
  16. :theme="action === 'disliked' ? 'filled' : 'outlined'"
  17. @click="dislike"
  18. />
  19. </a-tooltip>
  20. <span style="padding-left: '8px';cursor: 'auto'">
  21. {{ dislikes }}
  22. </span>
  23. </span>
  24. <span key="comment-basic-reply-to">Reply to</span>
  25. </template>
  26. <a slot="author">Han Solo</a>
  27. <a-avatar
  28. slot="avatar"
  29. src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  30. alt="Han Solo"
  31. />
  32. <p slot="content">
  33. We supply a series of design principles, practical patterns and high quality design resources
  34. (Sketch and Axure), to help people create their product prototypes beautifully and
  35. efficiently.
  36. </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评论 - 图3

配合 List 组件

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

  1. <template>
  2. <a-list
  3. class="comment-list"
  4. :header="`${data.length} replies`"
  5. item-layout="horizontal"
  6. :data-source="data"
  7. >
  8. <a-list-item slot="renderItem" slot-scope="item, index">
  9. <a-comment :author="item.author" :avatar="item.avatar">
  10. <template slot="actions">
  11. <span v-for="action in item.actions">{{ action }}</span>
  12. </template>
  13. <p slot="content">
  14. {{ item.content }}
  15. </p>
  16. <a-tooltip slot="datetime" :title="item.datetime.format('YYYY-MM-DD HH:mm:ss')">
  17. <span>{{ item.datetime.fromNow() }}</span>
  18. </a-tooltip>
  19. </a-comment>
  20. </a-list-item>
  21. </a-list>
  22. </template>
  23. <script>
  24. import moment from 'moment';
  25. export default {
  26. data() {
  27. return {
  28. data: [
  29. {
  30. actions: ['Reply to'],
  31. author: 'Han Solo',
  32. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  33. content:
  34. '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:
  42. '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.',
  43. datetime: moment().subtract(2, 'days'),
  44. },
  45. ],
  46. moment,
  47. };
  48. },
  49. };
  50. </script>

Comment评论 - 图4

嵌套评论

评论可以嵌套。

  1. <template>
  2. <a-comment>
  3. <span slot="actions" key="comment-nested-reply-to">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">
  11. We supply a series of design principles, practical patterns and high quality design resources
  12. (Sketch and Axure).
  13. </p>
  14. <a-comment>
  15. <span slot="actions">Reply to</span>
  16. <a slot="author">Han Solo</a>
  17. <a-avatar
  18. slot="avatar"
  19. src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  20. alt="Han Solo"
  21. />
  22. <p slot="content">
  23. We supply a series of design principles, practical patterns and high quality design
  24. resources (Sketch and Axure).
  25. </p>
  26. <a-comment>
  27. <span slot="actions">Reply to</span>
  28. <a slot="author">Han Solo</a>
  29. <a-avatar
  30. slot="avatar"
  31. src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  32. alt="Han Solo"
  33. />
  34. <p slot="content">
  35. We supply a series of design principles, practical patterns and high quality design
  36. resources (Sketch and Axure).
  37. </p>
  38. </a-comment>
  39. <a-comment>
  40. <span slot="actions">Reply to</span>
  41. <a slot="author">Han Solo</a>
  42. <a-avatar
  43. slot="avatar"
  44. src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  45. alt="Han Solo"
  46. />
  47. <p slot="content">
  48. We supply a series of design principles, practical patterns and high quality design
  49. resources (Sketch and Axure).
  50. </p>
  51. </a-comment>
  52. </a-comment>
  53. </a-comment>
  54. </template>

Comment评论 - 图5

回复框

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

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

API

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