【问题标题】:Angular 5 - Remove a child component from parent view on user deleteAngular 5 - 在用户删除时从父视图中删除子组件
【发布时间】:2017-12-06 11:50:33
【问题描述】:

我有 2 个组件,一个 Moment(post/thread) 和一个 Comment,每个 moment 可以有多个 cmets。

在moment-details.component 中,我插入了一个moment-comment.component。评论组件显示与该时刻相关的所有 cmets。

moments-detail.component.html

<div id="moment">

...

            <app-moment-comment [moment]="moment" [auth]="auth" [comment]="comment" class="comment" *ngFor="let comment of moment?.comments; let i=index">
            </app-moment-comment>


        </div>
    </div>
</div>

moment-detail.component.ts

@Component({
    selector: 'app-moment-detail',
    templateUrl: './moment-detail.component.html',
    styleUrls: ['./moment-detail.component.scss']
})
export class MomentDetailComponent implements OnInit {

    moment: Moment;

    constructor(private router: Router,
        private route: ActivatedRoute,
        private momentService: MomentService,
        private authService: AuthService,
        private commentService: CommentService) { }

    ngOnInit() {

        this.route.params.switchMap((params) => {
            let moment_id = params['id'];
            return this.momentService.get(moment_id);
        }).subscribe((res) => {
            console.log(res);
            this.moment = res;
            this.user = res.author;
        });

        this.authService.getAuthenticatedUser().subscribe((res) => {
            this.auth = res;
        });

    }
}

moment-comment 循环遍历所有 cmets 并创建一个新的 moment-comment 组件实例。

现在在 moment-comment.component.ts 中,用户可以触发删除他们的评论。

@Component({
    selector: 'app-moment-comment',
    templateUrl: './moment-comment.component.html',
    styleUrls: ['./moment-comment.component.scss']
})
export class MomentCommentComponent implements OnInit {

    @Input() comment: Comment;
    @Input() auth: any;
    @Input() moment: any;
    showReply: any;
    reply: any;
    showReplies: boolean;
    inEdit = false;
    editComment: any;

    constructor(private commentService: CommentService) { }

...
    delete() {

        this.commentService.delete(this.moment._id, this.comment._id).subscribe((res) => {
            console.log(res);
        }, (err) => {
            console.log(err);
        });

    }
...
}

果然,当用户触发 delete() 函数时,后端会更新我的数据库以显示评论已被删除。

我的问题是,如何在前端(即父级(时刻细节)视图)中反映这种变化?

【问题讨论】:

  • 更新对象以更新ui
  • @Aravind ye 我猜是这样的,但是从 moment-comment.component 中,我无法访问整个 moment?.comments,正如您在 ngFor 中的 moment.component 中看到的那样.我每次都通过 1 条评论。
  • 您能否在plunkerstackblitz 中重现该错误,以便我帮助您修复它
  • 你现在怎么样了?.cmets??
  • 能把moment.detail组件ts也发一下吗?

标签: angular


【解决方案1】:

您需要从评论组件向其父组件(即详细信息组件)发出事件并删除评论 来自数组的详细组件如下:

moments-detail.component.html

<div id="moment">

...
        <app-moment-comment (onDelete)="removeComment(i)" [index]="i" 
            [moment]="moment" [auth]="auth" [comment]="comment" class="comment" 
            *ngFor="let comment of moment?.comments; let i=index">
        </app-moment-comment>

        </div>
    </div>
</div>

moments-detail.component.ts

removeComment(index: number) {
    this.moment.comments.splice(index, 1);
}

moment-comment.component.ts

...
@Output() public onDelete: EventEmitter<any> = new EventEmitter();
@Input() comment: Comment;
...

delete() {
    this.commentService.delete(this.moment._id, this.comment._id).subscribe((res) => {
        console.log(res);
        this.onDelete.emit();
    }, (err) => {
        console.log(err);
    });

}

【讨论】:

  • 哦,EventEmitters 现在我明白了它的用途。我在实现这个和那个时遇到了一个小问题,当我调用 this.onDelete() 时,它说“无法调用类型缺少调用签名的表达式。类型 EventEmitter 没有兼容的调用签名”
  • 啊它的 this.onDelete.emit()
  • 您对这里的答案有何看法 - stackoverflow.com/questions/36076700/… 这是事件发射器的有效用例,因为我们没有在服务中使用它?
  • 如果他正在使用一个服务,他不需要从子组件发出它
  • @KHAN 很抱歉忘记了.emit(),我实际上并没有测试代码。是的,我认为我们的用例是一个有效的用例,因为我们正在进行子组件与父组件的通信。
猜你喜欢
  • 2018-09-15
  • 2018-08-27
  • 1970-01-01
  • 2011-12-06
  • 1970-01-01
  • 2011-10-15
  • 2011-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多