我在互联网上找到了角度主题的简单例子。我尝试使用示例中的代码,使用subject但我没有获得workTime变量的任何结果。问题出在哪里?
Work.service.ts
import { Subject } from "rxjs/Subject";
import "rxjs/Rx";
@Injectable()
export class WorkService {
public $workTimeSubject: Subject<number> = new Subject();
constructor() { }
}
app.component.ts:
export class AppComponent implements OnInit{
constructor(private _workService: WorkService) { }
ngOnInit() {
this._workService.$workTimeSubject.next(40);
console.log('app init');
this._workService.setWorkTime(40);
}
}
Boss.component.ts:
export class BossComponent implements OnInit {
workTime: number;
constructor(private _workService: WorkService) { }
ngOnInit() {
this._workService.$workTimeSubject.subscribe((value) => {
this.workTime = value;
console.log('value', value);
});
}
}
boss.component.ts
{{ workTime }}
答案 0 :(得分:2)
您的代码中很少有修改,请检查
Work.service.ts:
import { Subject } from "rxjs/Subject";
import "rxjs/Rx";
@Injectable()
export class WorkService {
private $workTimeSubject: Subject<number> = new Subject();
updateWorkTime(time: number) {
this.$workTimeSubject.next(time);
}
getWorkTime(): Observable<any> {
return this.$workTimeSubject.asObservable();
}
}
app.component.ts:
export class AppComponent implements OnInit{
constructor(private _workService: WorkService) { }
ngOnInit() {
this._workService.updateWorkTime(40);
console.log('app init');
}
}
<强> Boss.component.ts:强>
export class BossComponent implements OnInit {
workTime: number;
subscription: Subscription;
constructor(private _workService: WorkService) {
this.subscription = this._workService.getWorkTime().subscribe(time => { this.workTime = time; });
}
}
答案 1 :(得分:0)
ORDER BY row_number() OVER (ORDER BY [Project1].[Id] ASC)
OFFSET 100 ROWS FETCH NEXT 100 ROWS ONLY
在AppComponent
有机会订阅之前发出值。如果您希望主题向新订阅者重播上次发出的值,请使用BossComponent
。
替换:
ReplaySubject
使用:
public $workTimeSubject: Subject<number> = new Subject();