简体   繁体   English

如何从父组件到子组件发射数据[Angular 2]

[英]How to emit data from parent to child component [Angular 2]

From parent component I do like this: 从父组件我喜欢这样:

import {Observable, Subscription, Subject} from 'rxjs';

filteredBanks: Subject<any> = new Subject();
cilickFun() {
 this.filteredBanks.next('some value');
}

And child component: 和子组件:

@Input() filteredBanks: Subject<any>;

ngOnInit() {

        this.filteredBanks.subscribe(event => {
            console.log(event);
        });

}

I get error in console: Cannot read property 'subscribe' of undefined 我在控制台中收到错误: Cannot read property 'subscribe' of undefined

Anyone know how to solve this? 有人知道如何解决吗?

EDIT 编辑

parent template: 父模板:

<a (click)="cilickFun()"> EMIT</a>

<div class="col-md-9">
      <app-singlebank></app-singlebank>
</div> 

In your parent template it should be 在您的父模板中,它应该是

<a (click)="cilickFun()"> EMIT</a>

<div class="col-md-9">
      <app-singlebank [filteredBanks]='filteredBanks'></app-singlebank>
</div> 

I assume that app-singlebank is the selector for child component 我假设app-singlebank是子组件的选择器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM