提问人:devguy 提问时间:4/26/2017 更新时间:4/27/2017 访问量:741
RxJS startWith 在 Angular 4 应用程序中生成 TypeScript 编译器错误
RxJS startWith generates a TypeScript compiler error in Angular 4 app
问:
当我使用 .但是,如果我将这个(初始)值移动到运算符中,一切都会编译并运行正常。startWith
scan
我无法找出错误的根源。我的 Angular 4 测试应用程序中的示例代码如下所示。
编译错误:
rxjs-counter: master$ ng s
** NG Live Development Server is running on http://localhost:4200 **
Hash: ef256d342c83fd7c92a6
Time: 15133ms
chunk {0} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 165 kB {4} [initial] [rendered]
chunk {1} main.bundle.js, main.bundle.js.map (main) 5.48 kB {3} [initial] [rendered]
chunk {2} styles.bundle.js, styles.bundle.js.map (styles) 9.77 kB {4} [initial] [rendered]
chunk {3} vendor.bundle.js, vendor.bundle.js.map (vendor) 2.98 MB [initial] [rendered]
chunk {4} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered]
ERROR in /home/yadav/dev/javascript/rxjs-counter/src/app/app.component.ts (46,19): Argument of type '{ count: number; }' is not
assignable to parameter of type 'IScheduler | ((v: CounterData) => { count: number; })'.
Object literal may only specify known properties, and 'count' does not exist in type 'IScheduler | ((v: CounterData) => { coun
t: number; })'.
webpack: Failed to compile.
示例模板:(app.component.html)
<h1>
{{title}}
</h1>
<div>{{count}}</div>
<div>
<button (click)="onStart()">Start</button>
<button (click)="onStop()">Stop</button>
<button (click)="onReset()">Reset</button>
</div>
示例代码:(app.component.ts)
import { Component } from '@angular/core';
import { Observable, Subject } from 'rxjs/Rx';
interface CounterData {
count: number;
}
interface CounterFunc {
(arg: CounterData);
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Reactive Counter';
count = 0;
// Observable (streams)
start$: Subject<number>;
stop$: Subject<number>;
reset$: Subject<number>;
counter$: Observable<CounterData>;
intervalUntil$: Observable<number>;
constructor() {
this.start$ = new Subject();
this.stop$ = new Subject();
this.reset$ = new Subject();
// Observable that cancels on a stream.
this.intervalUntil$ = Observable
.interval(500)
.takeUntil(this.stop$);
// Notice that takeUntil is on the inner Observable, putting it on the outer Observable
// would kill the entire Observable chain, and we would need to re-subscribe on it again.
this.counter$ = this.start$
.switchMapTo(Observable.merge(
this.intervalUntil$.mapTo(this.incCount),
this.reset$.mapTo(this.resetCount)
))
.startWith({count: 0})
.scan((acc: CounterData, curr: CounterFunc) => curr(acc));
// Assign observer to cancelable interval stream.
this.counter$
.subscribe((v: CounterData) => {
this.count = v.count;
console.log(v);
});
}
resetCount(v: CounterData) {
return {count: 0};
}
incCount(v: CounterData) {
return {count: v.count + 1};
}
onStart() {
this.start$.next();
}
onStop() {
this.stop$.next();
}
onReset() {
this.reset$.next();
}
}
答:
1赞
martin
4/27/2017
#1
您没有使用种子值进行初始化。http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-scanscan()
您需要像这样设置它:
.scan((acc: CounterData, curr: CounterFunc) => curr(acc), {count: 0});
此外,现在您根本不需要使用。startWith
评论
0赞
devguy
4/28/2017
startWith 的作用与种子相同,使用种子值不会将初始值推送给观察者,这就是需要使用 startWith 的原因。
0赞
martin
4/28/2017
不,它没有。scan() 运算符需要一个种子值,因为这是 “acc” 参数初始化时使用的值。然后,startWith() 中的值在第一个回调调用中作为“curr”传递。
0赞
devguy
4/28/2017
感谢您消除我的困惑,解决了问题,现在明白为什么我遇到编译器错误了。
评论