对于相同组件
@input API
不需要。当您希望将数据从父组件传递到子组件时,将使用它。
//output.component.html
<p *ngFor="let data of dataService.datas" > // removed [data]="data" and added dataService.datas
<p>{{data?.name}}</p>
</p> //changed the position of </p>
export class OutputComponent implements OnInit {
constructor(private dataService: DataService) {}
}
export class DataService {
datas= [];
addData(name: string){
return this.datas.push({name: name}); //return keyword was missing
}
}
仅供参考
演示:
https://plnkr.co/edit/XlJM2LHFwlAYpQe2ancM?p=preview