类型"计数器"的参数不能分配给类型为"(状态:AppState) => number



Angular,ngrx和TypeScript的新手,我正在按照ngrx GitHub页面上的示例来学习基础知识。但是,我在遵循简单的应用程序时遇到了此错误:argument of type '"counter"' is not assignable to parameter of type '(state: AppState) => number'.

我的代码在下面,错误发生在constructor内的第 24 行:

import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { INCREMENT, DECREMENT, RESET } from './counter';
interface AppState {
counter: number;
}
@Component({
selector: 'app-counter',
template: `
<button (click)="increment()">Increment</button>
<div>Current Count: {{counter | async}}</div>
<button (click)="decrement()">Decrement</button>
<button (click)="reset()">Reset</button>
`
})
export class CounterComponent {
counter: Observable<number>;
constructor(private store: Store<AppState>) {
this.counter = store.select<number>('counter');
}
increment() {
this.store.dispatch({type: INCREMENT});
}
decrement() {
this.store.dispatch({type: DECREMENT});
}
reset(){
this.store.dispatch({type: RESET});
}
}

我是否在Observablenumber之间缺少一些类型转换?任何提示将不胜感激。

附言找到了我的问题的解决方案,我将构造器内部的 24 行更改为this.counter = this.store.select(AppState => AppState.counter);,一切正常。我不确定它为什么有效,仍然试图理解整个事情。

找到了我的问题的解决方案,我将构造器内部的第 24 行更改为 this.counter = this.store.select(AppState => AppState.counter(; 一切都很好。我不确定它为什么有效,仍然试图理解整个事情。

相关内容

最新更新