Issue in implementing the Ngrx Store service into my component

无人久伴 提交于 2019-12-12 05:39:55

问题


After a user tried to help with an issue regarding that post: My stackoverflow old post I am now trying to implement the Ngrx store using Ngrx store github to help me solve multiples input/output event.

Just after my constructor I have an error:

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;
}

}


回答1:


You have set the initial state to false in your reducer. Make it any and initialize it with empty object. Also in your switch case for ON/OFF/RESET etc. you have to return immutable state like below:

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


来源:https://stackoverflow.com/questions/45304107/issue-in-implementing-the-ngrx-store-service-into-my-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!