0

在用户尝试帮助解决有关该帖子的问题后:我的 stackoverflow 旧帖子我现在正在尝试使用Ngrx 商店 github实现 Ngrx 商店,以帮助我解决多个输入/输出事件。

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

counter不可分配给类型参数(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');
  }

  //...
}

计数器.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;
}

}

4

1 回答 1

1

您已在减速器中将初始状态设置为 false。将其设为任何并用空对象对其进行初始化。此外,在您的开关盒中用于 ON/OFF/RESET 等,您必须返回不可变状态,如下所示:

return {...state, counter:true/false/0}
于 2018-02-14T12:24:24.760 回答