Angular 在不相关的组件之间传递数据
在我的Angular 在不相关的组件之间传递数据,angular,Angular,在我的新闻提要中,我有三个组件(新闻提要、评论、回复)。组件我正在这样做: <div *ngFor="let item of data" id="{{item.post_id}}" class="post_{{item.post_id}}"> //news feed data is fine here but without comments and replys </div> //这里的新闻提要数据很好,但是没有评论和回复 我在寻找这个想法: <div
新闻提要中,我有三个组件(新闻提要、评论、回复)。组件我正在这样做:
<div *ngFor="let item of data" id="{{item.post_id}}" class="post_{{item.post_id}}">
//news feed data is fine here but without comments and replys
</div>
//这里的新闻提要数据很好,但是没有评论和回复
我在寻找这个想法:
<div *ngFor="let item of data" id="{{item.post_id}}" class="post_{{item.post_id}}">
<app-comment>
<app-reply></app-reply>
</app-comment>
<div>
如何将{{item.post\u id}}
传递给其他组件
注意:评论和回复组件不是新闻提要的子组件。您要找的是decorator。在子组件中,按如下方式使用它:
// app-comment.component.ts
import { Component, OnInit, Input } from '@angular/core';
@Component(...)
export class AppCommentComponent implements OnInit {
@Input('someKey') someKey: any;
constructor() {}
ngOnInit() {
console.log(this.someKey);
}
}
您可以像这样将数据传递给它:
<app-comment [someKey]="'someValue'"></app-comment>
选中此项,您需要使用@input。。检查这一点,他特别指出接收方组件不是发送方的子组件。我认为,他需要一个基于服务的体系结构,在组件之间有一个服务,为两者提供数据。