Ngrx效果不听动作

时间:2019-03-14 11:56:25

标签: angular ngrx ngrx-effects

我想在我的角度应用程序(v7)中使用ngrx / efffects和ngrx / store。我正在ngrx documentation中建议在我的应用中进行配置。在AppComponent中分派动作时,reducer可以按预期工作,但效果未监听任何动作

测试:

执行ng serve -o后,请检查控制台。一切都按预期方式记录,除了效果中的一个。

请提示我哪里出了问题。

Github Repo

效果

@Injectable()
export class AppEffects {
  constructor(
    private actions$: Actions,
    private myService: MyserviceService
  ) {}

  myAction$ = this.actions$.pipe(
    ofType<fromActions.GetItems>(MyActionTypes.GET_ITEMS),
    switchMap(action => {
      console.log('i am in effects', action);
      return this.myService.getItems().pipe(
        map(data => new fromActions.GetItemsSuccess(data)),
        catchError(err => of(new fromActions.GetItemsSuccess(err)))
      );
    })
  );
}

在AppModule中配置的效果

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    StoreModule.forRoot(reducers, { metaReducers }),
    EffectsModule.forRoot([AppEffects])
  ],
})

在AppComponent中调度动作

export class AppComponent implements OnInit {
  constructor(private store: Store<State>) {
    console.log('in component');
  }

  ngOnInit() {
    console.log('dispatched action in AppComponent');
    this.store.dispatch(new fromAction.GetItems());
  }
}

1 个答案:

答案 0 :(得分:0)

您缺少@Effect()装饰器,没有该装饰器,这些可观察物将不会被注册为商店的副作用。

public void setAllRoles()
{
thrshld = 0;
ObservableList<GroupEntry> groups = this.list2.getItems();
for (GroupEntry item : groups) {
  thrshld--;
}
thrshldMax = thrshld;

if (this.allRoles) {
  this.allRoles = false;
  for (GroupEntry item : groups) {
    item.setSelected(new SimpleBooleanProperty(false));
    item.isSelected().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> ov,
          Boolean old_val, Boolean new_val) {
            BooleanProperty thatCB = item.isSelected();
            if (thatCB.getValue() == true) {
              checkbox2.setIndeterminate(true);
              thrshld++; // = thrshld + 1;
            } else {
              thrshld--; // = thrshld - 1;
            } 
            if (thrshld == thrshldMax) {
              checkbox2.setIndeterminate(false);
              checkbox2.setSelected(false);
            }
            if (thrshld == 0) {
              checkbox2.setIndeterminate(false);
              checkbox2.setSelected(true);
            }
            //status.setText("state: " +thatCB.getValue()+ "\r\nthrshld: " +thrshld+ "Max: " +thrshldMax);
          }
    });
  }
  this.list2.refresh();
} else {
  this.allRoles = true;
  thrshld = 0;
  for (GroupEntry item : groups) {
    item.setSelected(new SimpleBooleanProperty(true));
    item.isSelected().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> ov,
          Boolean old_val, Boolean new_val) {
            BooleanProperty thisCB = item.isSelected();
            if (thisCB.getValue() == true) {
              thrshld++; // = thrshld + 1;
              if (thrshld == 0) {
                checkbox2.setIndeterminate(false);
                checkbox2.setSelected(true);
              }
            } else {
              checkbox2.setIndeterminate(true);
              thrshld--; // = thrshld - 1;
              if (thrshld == thrshldMax) {
                checkbox2.setIndeterminate(false);
                checkbox2.setSelected(false);
              }
            }
            //status.setText("state: " +thisCB.getValue()+ "\r\nthrshld: " +thrshld+ "Max: " +thrshldMax);
          }
    });
  }
  this.list2.refresh();
 }
}