Comment评论

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

何时使用

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

代码演示

Comment评论 - 图1

基本评论

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

  1. import { Component } from '@angular/core';
  2. import { distanceInWords } from 'date-fns';
  3. @Component({
  4. selector: 'nz-demo-comment-basic',
  5. template: `
  6. <nz-comment nzAuthor="Han Solo" [nzDatetime]="time">
  7. <nz-avatar
  8. nz-comment-avatar
  9. nzIcon="user"
  10. nzSrc="//zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  11. ></nz-avatar>
  12. <nz-comment-content>
  13. <p>
  14. We supply a series of design principles, practical patterns and high quality design resources (Sketch and
  15. Axure), to help people create their product prototypes beautifully and efficiently.
  16. </p>
  17. </nz-comment-content>
  18. <nz-comment-action>
  19. <i
  20. nz-tooltip
  21. nzTitle="Like"
  22. nz-icon
  23. type="like"
  24. [theme]="likes > 0 ? 'twotone' : 'outline'"
  25. (click)="like()"
  26. ></i>
  27. <span class="count like">{{ likes }}</span>
  28. </nz-comment-action>
  29. <nz-comment-action>
  30. <i
  31. nz-tooltip
  32. nzTitle="Dislike"
  33. nz-icon
  34. type="dislike"
  35. [theme]="dislikes > 0 ? 'twotone' : 'outline'"
  36. (click)="dislike()"
  37. ></i>
  38. <span class="count dislike">{{ dislikes }}</span>
  39. </nz-comment-action>
  40. <nz-comment-action>Reply to</nz-comment-action>
  41. </nz-comment>
  42. `,
  43. styles: [
  44. `
  45. .count {
  46. padding-left: 8px;
  47. cursor: auto;
  48. }
  49. `
  50. ]
  51. })
  52. export class NzDemoCommentBasicComponent {
  53. likes = 0;
  54. dislikes = 0;
  55. time = distanceInWords(new Date(), new Date());
  56. like(): void {
  57. this.likes = 1;
  58. this.dislikes = 0;
  59. }
  60. dislike(): void {
  61. this.likes = 0;
  62. this.dislikes = 1;
  63. }
  64. }

Comment评论 - 图2

配合列表组件

配合 nz-list 组件展现评论列表。

  1. import { Component } from '@angular/core';
  2. import { addDays, distanceInWords } from 'date-fns';
  3. @Component({
  4. selector: 'nz-demo-comment-list',
  5. template: `
  6. <nz-list [nzDataSource]="data" [nzRenderItem]="item" [nzItemLayout]="'horizontal'">
  7. <ng-template #item let-item>
  8. <nz-comment [nzAuthor]="item.author" [nzDatetime]="item.datetime">
  9. <nz-avatar nz-comment-avatar nzIcon="user" [nzSrc]="item.avatar"></nz-avatar>
  10. <nz-comment-content>
  11. <p>{{ item.content }}</p>
  12. </nz-comment-content>
  13. <nz-comment-action>Reply to</nz-comment-action>
  14. </nz-comment>
  15. </ng-template>
  16. </nz-list>
  17. `,
  18. styles: []
  19. })
  20. export class NzDemoCommentListComponent {
  21. data = [
  22. {
  23. author: 'Han Solo',
  24. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  25. content:
  26. 'We supply a series of design principles, practical patterns and high quality design resources' +
  27. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
  28. datetime: distanceInWords(new Date(), addDays(new Date(), 1))
  29. },
  30. {
  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' +
  35. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
  36. datetime: distanceInWords(new Date(), addDays(new Date(), 2))
  37. }
  38. ];
  39. }

Comment评论 - 图3

嵌套评论

评论可以嵌套。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-comment-nested',
  4. template: `
  5. <ng-template #commentTemplateRef let-comment="comment">
  6. <nz-comment [nzAuthor]="comment.author">
  7. <nz-avatar nz-comment-avatar nzIcon="user" [nzSrc]="comment.avatar"></nz-avatar>
  8. <nz-comment-content>
  9. <p>{{ comment.content }}</p>
  10. </nz-comment-content>
  11. <nz-comment-action>Reply to</nz-comment-action>
  12. <ng-container *ngIf="comment.children && comment.children.length">
  13. <ng-template ngFor let-child [ngForOf]="comment.children">
  14. <ng-template [ngTemplateOutlet]="commentTemplateRef" [ngTemplateOutletContext]="{ comment: child }">
  15. </ng-template>
  16. </ng-template>
  17. </ng-container>
  18. </nz-comment>
  19. </ng-template>
  20. <ng-template [ngTemplateOutlet]="commentTemplateRef" [ngTemplateOutletContext]="{ comment: data }"> </ng-template>
  21. `,
  22. styles: []
  23. })
  24. export class NzDemoCommentNestedComponent {
  25. data = {
  26. author: 'Han Solo',
  27. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  28. content:
  29. 'We supply a series of design principles, practical patterns and high quality design resources' +
  30. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
  31. children: [
  32. {
  33. author: 'Han Solo',
  34. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  35. content:
  36. 'We supply a series of design principles, practical patterns and high quality design resources' +
  37. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
  38. children: [
  39. {
  40. author: 'Han Solo',
  41. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  42. content:
  43. 'We supply a series of design principles, practical patterns and high quality design resources' +
  44. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.'
  45. },
  46. {
  47. author: 'Han Solo',
  48. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  49. content:
  50. 'We supply a series of design principles, practical patterns and high quality design resources' +
  51. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.'
  52. }
  53. ]
  54. }
  55. ]
  56. };
  57. }

Comment评论 - 图4

回复框

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

  1. import { Component } from '@angular/core';
  2. import { distanceInWords } from 'date-fns';
  3. @Component({
  4. selector: 'nz-demo-comment-editor',
  5. template: `
  6. <nz-list *ngIf="data.length" [nzDataSource]="data" [nzRenderItem]="item" [nzItemLayout]="'horizontal'">
  7. <ng-template #item let-item>
  8. <nz-comment [nzAuthor]="item.author" [nzDatetime]="item.displayTime">
  9. <nz-avatar nz-comment-avatar nzIcon="user" [nzSrc]="item.avatar"></nz-avatar>
  10. <nz-comment-content>
  11. <p>{{ item.content }}</p>
  12. </nz-comment-content>
  13. </nz-comment>
  14. </ng-template>
  15. </nz-list>
  16. <nz-comment>
  17. <nz-avatar nz-comment-avatar nzIcon="user" [nzSrc]="user.avatar"></nz-avatar>
  18. <nz-comment-content>
  19. <nz-form-item>
  20. <textarea [(ngModel)]="inputValue" nz-input rows="4"></textarea>
  21. </nz-form-item>
  22. <nz-form-item>
  23. <button nz-button nzType="primary" [nzLoading]="submitting" [disabled]="!inputValue" (click)="handleSubmit()">
  24. Add Comment
  25. </button>
  26. </nz-form-item>
  27. </nz-comment-content>
  28. </nz-comment>
  29. `,
  30. styles: []
  31. })
  32. export class NzDemoCommentEditorComponent {
  33. data: any[] = [];
  34. submitting = false;
  35. user = {
  36. author: 'Han Solo',
  37. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png'
  38. };
  39. inputValue = '';
  40. handleSubmit(): void {
  41. this.submitting = true;
  42. const content = this.inputValue;
  43. this.inputValue = '';
  44. setTimeout(() => {
  45. this.submitting = false;
  46. this.data = [
  47. ...this.data,
  48. {
  49. ...this.user,
  50. content,
  51. datetime: new Date(),
  52. displayTime: distanceInWords(new Date(), new Date())
  53. }
  54. ].map(e => {
  55. return {
  56. ...e,
  57. displayTime: distanceInWords(new Date(), e.datetime)
  58. };
  59. });
  60. }, 800);
  61. }
  62. }

API

PropertyDescriptionTypeDefault
[nzAuthor]显示评论的作者string|TemplateRef<void>-
[nzDatetime]展示时间描述string|TemplateRef<void>-

单独引入此组件

想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。

  1. import { NzCommentModule } from 'ng-zorro-antd';

评论组成部分

元素说明
<nz-avatar nz-comment-avatar>要显示为评论头像的元素
<nz-comment-content>评论的主要内容
<nz-comment-action>在评论内容下面呈现的操作项