• Comment评论
    • 何时使用
    • 单独引入此组件
    • 代码演示
    • API
      • nz-commentcomponent
      • [nz-comment-avatar]directive
      • nz-comment-contentcomponent
      • nz-comment-actioncomponent

    Comment评论

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

    何时使用

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

    单独引入此组件

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

    1. import { NzCommentModule } from 'ng-zorro-antd/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. })
    19. export class NzDemoCommentListComponent {
    20. data = [
    21. {
    22. author: 'Han Solo',
    23. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
    24. content:
    25. 'We supply a series of design principles, practical patterns and high quality design resources' +
    26. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
    27. datetime: distanceInWords(new Date(), addDays(new Date(), 1))
    28. },
    29. {
    30. author: 'Han Solo',
    31. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
    32. 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 efficiently.',
    35. datetime: distanceInWords(new Date(), addDays(new Date(), 2))
    36. }
    37. ];
    38. }

    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. })
    23. export class NzDemoCommentNestedComponent {
    24. data = {
    25. author: 'Han Solo',
    26. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
    27. content:
    28. 'We supply a series of design principles, practical patterns and high quality design resources' +
    29. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
    30. children: [
    31. {
    32. author: 'Han Solo',
    33. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
    34. content:
    35. 'We supply a series of design principles, practical patterns and high quality design resources' +
    36. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
    37. children: [
    38. {
    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' +
    43. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.'
    44. },
    45. {
    46. author: 'Han Solo',
    47. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
    48. content:
    49. 'We supply a series of design principles, practical patterns and high quality design resources' +
    50. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.'
    51. }
    52. ]
    53. }
    54. ]
    55. };
    56. }

    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. })
    31. export class NzDemoCommentEditorComponent {
    32. data: any[] = [];
    33. submitting = false;
    34. user = {
    35. author: 'Han Solo',
    36. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png'
    37. };
    38. inputValue = '';
    39. handleSubmit(): void {
    40. this.submitting = true;
    41. const content = this.inputValue;
    42. this.inputValue = '';
    43. setTimeout(() => {
    44. this.submitting = false;
    45. this.data = [
    46. ...this.data,
    47. {
    48. ...this.user,
    49. content,
    50. datetime: new Date(),
    51. displayTime: distanceInWords(new Date(), new Date())
    52. }
    53. ].map(e => {
    54. return {
    55. ...e,
    56. displayTime: distanceInWords(new Date(), e.datetime)
    57. };
    58. });
    59. }, 800);
    60. }
    61. }

    API

    nz-commentcomponent

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

    [nz-comment-avatar]directive

    要显示为评论头像的元素。

    nz-comment-contentcomponent

    评论的主要内容。

    nz-comment-actioncomponent

    在评论内容下面呈现的操作项。