代码之家  ›  专栏  ›  技术社区  ›  Johan Rin

发出的值不会随着EventEmitter弹出气泡

  •  0
  • Johan Rin  · 技术社区  · 6 年前

    当我试图传递方法时,出现了下面的错误消息 onDeleteComment 到另一个组件。

    messageId=[对象对象]

    错误类型错误:无法读取未定义的属性“text”

    消息-item.component.ts

    onDeleteComment(messageId: string, comment: Comment) {
      /* For debugging purpose only x*/
      console.log('messageId = ' + messageId);
      console.log('comment.text = ' + comment.text);
    
      //this.commentService.deleteMessageComment(messageId, comment);
    }
    

    消息-item.component.html

    <div class="component-container" *ngIf="message$ | async as message; else loading">
    
      /* ... */
    
      <div *ngFor="let comment of message.comments">
        <app-comments-item
          [commentId]="comment"
          (deleteComment)="onDeleteComment($event)"
          [sourceId]="message._id">
        </app-comments-item>
      </div>
    
    </div>
    

    注释-item.component.ts

    export class CommentsItemComponent implements OnInit {
      comment$: Observable<Comment>;
      @Input() commentId: string;
      @Output() deleteComment = new EventEmitter<{sourceId: string, comment: Comment}>();
      @Input() sourceId: string;
    
      /* ... */
    
    }
    

    注释-item.component.html

    <div class="component-container" *ngIf="comment$ | async as comment; else loading">
      <div class="header-container">
    
        /* ... */
    
        <div class="buttons-container">
          <button mat-icon-button type="button" (click)="deleteComment.emit({sourceId: sourceId, comment: comment})">
          </button>
        </div>
      </div>
    
     /* ... */
    
    </div>
    

    如何将注释冒泡到“我的邮件”项组件?

    提前谢谢你的帮助。

    1 回复  |  直到 6 年前
        1
  •  0
  •   user184994    6 年前

    $event将是具有两个属性的对象。

    改变你的功能如下:

    onDeleteComment(event: {sourceId: string, comment: Comment}) {
      /* For debugging purpose only x*/
      console.log('messageId = ', event.sourceId);
      console.log('comment.text = ', event.comment.text);
    
      //this.commentService.deleteMessageComment(messageId, comment);
    }