2017-07-25 45 views
0

用户试图帮助处理关于该帖子的问题:My stackoverflow old post我现在试图使用Ngrx store github来实现Ngrx商店,以帮助我解决多个输入/输出事件。在将Ngrx商店服务实施到我的组件中的问题

只是我的构造后,我有一个错误:

counter is not assignable to parameter of type (state: Appstate) => boolean :

child.ts

import { Component, Input, Output, EventEmitter } from '@angular/core'; 
import { UserService3 } from '../user3.service'; 
import { Store } from '@ngrx/store'; 
import { Observable } from 'rxjs/Observable'; 
import { ON, OFF, RESET } from '../counter'; 

interface AppState { 
    counter: boolean; 
} 

@Component({ 
    selector: 'my-daydetail', 
    templateUrl: './my-daydetail.component.html', 
    styleUrls: ['./my-daydetail.component.css'] 
}) 
export class MyDaydetailComponent { 

    counter: Observable<boolean>; 

    constructor(private store: Store<AppState>) { 
    this.counter = store.select('counter'); 
    } 

    //... 
} 

counter.ts

import { Action } from '@ngrx/store'; 

export const ON = 'ON'; 
export const OFF = 'OFF'; 
export const RESET = 'RESET'; 

export function counterReducer(state: boolean = true, action: Action) { 
    switch (action.type) { 
    case ON: 
    return false; 

    case OFF: 
    return true; 

    case RESET: 
    return 0; 

    default: 
    return state; 
} 

}

+0

@PierreDuc也许应该使用'功能'而不是? –

+0

你做错了权利递增递减可用于布尔类型右 –

+0

@RahulSingh认为我gotcha看看我的更新后的帖子,按照你的指示,我想我实现了正确的:-),现在nGrx如何帮助我从我的问题旧帖子? –

回答

1

您在缩减器中将初始状态设置为false。做任何事,并用空对象初始化它。同样在你的开关情况下ON/OFF/RESET等你必须返回像下面这样的不可变状态:

return {...state, counter:true/false/0} 
+0

谢谢我会试试这个 –