3
角度2では、mySubject(コード参照)はcomplete()関数をコンパイルしますが、実行中はそのような関数がないというエラーが発生します。私はonComplete()をコンパイルすることができませんでした。ストリームが完了したBehaviorSubjectに信号を送る方法
import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import * as Rx from "rxjs";
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent {
myBehavior: any;
mySubject: BehaviorSubject<string>;
received = "nothing";
chatter: string[];
nxtChatter = 0;
constructor() {
this.myBehavior = new BehaviorSubject<string>("Behavior Subject Started");
this.chatter = [
"Four", "score", "and", "seven", "years", "ago"
]
}
Start() {
this.mySubject = this.myBehavior.subscribe(
(x) => { this.received = x;},
(err) => { this.received = "Error: " + err; },
() => { this.received = "Completed ... bye"; }
);
}
Send() {
this.mySubject.next(this.chatter[this.nxtChatter++]);
if (this.nxtChatter >= this.chatter.length) {
this.nxtChatter = 0;
this.mySubject.complete();
}
}
}