2017-07-31 30 views
1

新建角,NGRX和打字稿,我正在关注的ngrx GitHub页学习的基础知识上的例子。不过,我在遵循简单应用程序时遇到了此错误:argument of type '"counter"' is not assignable to parameter of type '(state: AppState) => number'的说法是不能分配给类型的参数“(状态:届时AppState)=>号”

我的代码如下,在第24行出现错误constructor内:

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之间的一些类型转换?任何提示将不胜感激。

P.S.发现了一个修复我的问题,我把内部构造器的第24行改为this.counter = this.store.select(AppState => AppState.counter);,一切工作正常。我不确定它为什么还能工作,仍然试图理解整个事情。

+1

发表您编辑的答案..然后问题表明具有一个答案..然后你可以接受它在适当的时候太.. – JGFMK

回答

1

找到了一个修复我的问题,我改变了第24行内部构造函数this.counter = this.store.select(AppState => AppState.counter);一切工作正常。我不确定它为什么还能工作,仍然试图理解整个事情。

相关问题