【问题标题】:Angular 5: Subscription isn't triggered on next valueAngular 5:下一个值不会触发订阅
【发布时间】:2017-11-14 16:18:12
【问题描述】:

当将下一个值推送到主题时,不会触发父组件中的订阅(当我将主题更改为我无法使用的 BehaviorSubject 时会触发,因为没有默认值)。

option.component.ts

import { Component, OnInit, Input } from '@angular/core';
import { Subject } from 'rxjs';

@Component({
  selector: 'app-option',
  templateUrl: './option.component.html'
})
export class OptionComponent implements OnInit {

    @Input() httpHeader;

    period: Subject<string> = new Subject<string>();

    constructor(){}

    ngOnInit(){}

    setPeriod(period: string){
        this.period.next(period);
        console.log('Next period: ' + period);
    }
}

option.component.html

<mat-radio-group [(ngModel)]="periode" (ngModelChange)="setPeriod($event)">
    <span *ngFor="let option of httpHeader.option"><mat-radio-button [checked]="option.default" [value]="option.param" >{{ option.desc }}</mat-radio-button><br /></span>
</mat-radio-group>

父组件:

periodeomzet.component.ts

import { Component, OnInit, Output } from '@angular/core';
import { Subscription } from 'rxjs';
import { httpService } from 'app/services/http.service';
import { OptionComponent } from 'app/units/option/option.component';

@Component({
  selector: 'app-periodeomzet',
    templateUrl: './periodeomzet.component.html',
    providers: [OptionComponent]
})
export class PeriodeomzetComponent implements OnInit {

    httpHeader: Object;
    subData: Subscription;
    subOptions: Subscription;
    period: string = 'period=d';

    constructor(
        private httpService: httpService,
        private option: OptionComponent
    ){}

    getReport(){        
        this.subData = this.httpService.getReport().subscribe((res: Response) => {
            this.httpHeader = res.json()['header'];
        });
    }

    ngOnInit(){
        this.subOptions = this.option.period.subscribe(period => {
            console.log('subOptions subscription fired');

            this.period = period;
            this.getReport();
            // Do other stuff
        });
    }

    ngOnDestroy(){
        this.subData.unsubscribe();
        this.subOptions.unsubscribe();
    }
}

periodeomzet.component.html

<app-option [httpHeader]="httpHeader"></app-option>

当我更改单选按钮的选择时,我希望在控制台中看到“子选项已触发”,但它只显示“下一个周期:...”。为什么它不起作用?

【问题讨论】:

  • 为什么要通过 DI 访问子组件,而不是绑定到 @Output 或通过服务进行通信?
  • 我不知道为什么我这样做,因为我是一个初学者。如果我让这两个组件通过服务进行通信,如果我有多个组件对,它们如何与服务的同一个实例通信?你能用@Output 举个例子吗?
  • 您尝试阅读文档了吗? angular.io/guide/component-interaction
  • 有罪:) ......
  • @jonrsharpe 我已经实现了 EventEmitter + @Output 方法并且可以工作。这对我来说是全新的,所以谢谢你指出这一点

标签: angular typescript rxjs


【解决方案1】:

您不能像现在这样注入组件。相反,您需要引用该组件并使用ViewChild(或ViewChildren)来选择它。

在你的模板中,你可以使用

<app-option #option [httpHeader]="httpHeader"></app-option>

那么,在周期部分:

@ViewChild('option') option: OptionComponent;

如果不想使用#option模板语法也可以根据组件类来选择:

@ViewChild(OptionComponent) option: OptionComponent;

【讨论】:

  • 谢谢。我在模板中添加了#option,在构造函数上方添加了@ViewChild('option') option: OptionComponent;,并从TS中删除了提供程序,但是属性'period'无法读取undefined
  • @Rogier 你是否也删除了OptionComponent 的注入?
  • 我想我做到了。我从组件装饰器中删除了provider: [OptionComponent]
  • @Rogier 你也把它从构造函数中删除了?
猜你喜欢
  • 2019-09-22
  • 2023-01-01
  • 2020-03-15
  • 1970-01-01
  • 2018-07-27
  • 2021-04-24
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多