Yog*_*ear 4 rxjs behaviorsubject angular
在angular 2中,mySubject(参见代码)编译了一个complete()函数,但是在执行期间它没有错误,说没有这样的函数.我无法使用onComplete()进行编译.
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这一行:
this.mySubject = this.myBehavior.subscribe(
Run Code Online (Sandbox Code Playgroud)
返回订阅对象,而不是主题.订阅没有complete或next功能.要触发complete主题,请执行以下操作:
this.myBehavior.complete();
Run Code Online (Sandbox Code Playgroud)
此外,您还在触发next订阅:
this.mySubject.next(this.chatter[this.nxtChatter++]);
Run Code Online (Sandbox Code Playgroud)
您需要在主题上触发它:
this.myBehavior.next(this.chatter[this.nxtChatter++]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2398 次 |
| 最近记录: |