在我的组件中实现 Ngrx Store 服务的问题

Issue in implementing the Ngrx Store service into my component

在用户试图帮助解决有关 post 的问题后:My Whosebug old post I am now trying to implement the Ngrx store using Ngrx store github 帮助我解决多个 input/output 事件。

在我的构造函数之后我有一个错误:

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

}

您已在减速器中将初始状态设置为 false。使它成为任何并用空对象初始化它。同样在 ON/OFF/RESET 等的开关盒中,您必须 return 不可变状态,如下所示:

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