“"counter"”类型的参数不可分配给“(state: AppState) => number”类型的参数
argument of type '"counter"' is not assignable to parameter of type '(state: 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});
}
}
我是否遗漏了 Observable
或 number
之间的某些类型转换?任何提示将不胜感激。
P.S。找到了解决我的问题的方法,我将构造函数中的第 24 行更改为 this.counter = this.store.select(AppState => AppState.counter);
并且一切正常。我不确定它为什么起作用,仍在尝试理解整个事情。
找到了解决我的问题的方法,我将构造函数中的第 24 行更改为 this.counter = this.store.select(AppState => AppState.counter);一切正常。我不确定它为什么有效,仍在尝试理解整个事情。
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});
}
}
我是否遗漏了 Observable
或 number
之间的某些类型转换?任何提示将不胜感激。
P.S。找到了解决我的问题的方法,我将构造函数中的第 24 行更改为 this.counter = this.store.select(AppState => AppState.counter);
并且一切正常。我不确定它为什么起作用,仍在尝试理解整个事情。
找到了解决我的问题的方法,我将构造函数中的第 24 行更改为 this.counter = this.store.select(AppState => AppState.counter);一切正常。我不确定它为什么有效,仍在尝试理解整个事情。