订阅主题无济于事

时间:2018-10-04 14:28:49

标签: angular rxjs subject

我已经构建了一个服务,该服务在the answer provided by SrAxi之后的组件之间共享数据。一切工作正常,直到在接收器组件中实现对主题的订阅:响应和该订阅方法中的其他内容均未显示,既没有字符串的简单console.log;也没有任何内容。就像Angular根本没有输入该方法。

我试图对OnInit,AfterViewInit和AfterContentInit进行订阅:没有任何变化。还使用setTimeOut();什么都没有。

然后我找到了answer of Julius Dzidzevičius,然后将主题更改为behaviorSuject并订阅了该主题;再次没有改变。

这让我发疯,希望您能提供帮助

代码: 发件人组件

 private actionsInfoObj = {
    fromAction: null,
    showForm: null,
    data: null
};
.....
 addNewRow() {
    this.actionsInfoObj.fromAction = 'addRow';
    this.actionsInfoObj.showForm = true;
    this.bus.actionInfo(this.actionsInfoObj);
    this.addRow.emit( this.actionsInfoObj );
}

服务(ButtonsUtilitiesService)

getActionInfo$: Observable<any>;
   private getActionInfoSubject = new Subject<any>();

constructor() {
    this.getActionInfo$ = 
    this.getActionInfoSubject.asObservable();
}

actionInfo(data) {
    console.log(data);
    this.getActionInfoSubject.next(data);
}

接收器组件

constructor(
    private fb: FormBuilder,
    private bus: ButtonsUtilitiesService
) {
    this.bus.getActionInfo$.subscribe( (data: object) => {
        this.actionInfo = data;
    })
}

编辑:忘记了(暂时)发送者和接收者都在同一父组件中:

   <section *ngIf="!showForm ">
        <div>
            <app-table (addRow)="addRow($event)"></app-table>
        </div>
    </section>
    <section *ngIf="showForm"  class="mat-elevation-z8">
        <div>
            <app-dynamic-form></app-dynamic-form>
        </div>
    </section>

0 个答案:

没有答案