【发布时间】:2016-09-20 15:23:08
【问题描述】:
试图使这个Angular + ngrx store boiler 个人项目样板工作,但得到一个打字错误。错误消息非常简单,但如果不更改可观察对象的类型,我就无法解决它。首先,运行npm start时出现错误
app/app.component.ts(29,9): error TS2322: Type 'Observable<{}>' is not assignable to type 'Observable<number[]>'.
Type '{}' is not assignable to type 'number[]'.
Property 'length' is missing in type '{}'.
第 29 行在构造函数内部,是:
constructor(
private store: Store<AppState>
){
this.counter$ = store.select('counter'); // Line 29
}
在代码中,如果我将以下可观察类型更改为:
counter$: Observable<number>;
收件人:
counter$: Observable<any>;
npm start 在那里运行就像一个魅力,但我想知道为什么,因为我试图在 Obserbable 上强制使用数字类型
编辑:添加减速器代码:
import { ActionReducer, Action } from '@ngrx/store';
export const INCREMENT = 'INCREMENT';
export const counterReducer: ActionReducer<number> = (state: number = 0, action: Action) => {
switch (action.type) {
case INCREMENT:
return state + 1;
default:
return state;
}
}
【问题讨论】:
标签: angular rxjs observable ngrx