来自ngrx / entity的SelectAll不会选择任何内容

时间:2018-05-14 09:34:37

标签: angular typescript ngrx ngrx-entity

简介

我在让我的选择器工作时遇到了一些麻烦。我的组件不会使用FakeApiService中的图书进行更新,但我可以看到调用已完成。因此,行动和效果正在发挥作用。

我认为问题与我使用@ngrx/Entity有关。特别是这个

export const getBooksState = createFeatureSelector<DemoState>('demo');

export const { selectAll } = fromBook.adapter.getSelectors();

我已经看过他们为getSelectors()提供选择器功能的教程,但这不是必要的。

我希望有人可以发现我出错的地方,如果有人对结构/设置有一些建议,那我就是耳朵:)

这是我的设置。

版本

  • Angular:6.0.0
  • rxjs 6.1.0
  • typescript 2.7.2
  • webpack 4.6.0
  • @ngrx / effects 5.2.0
  • @ngrx / entity 5.2.0
  • @ngrx / router-store 5.2.0
  • @ngrx / schematics 5.2.0
  • @ngrx / store 5.2.0
  • @ngrx / store-devtools 5.2.0

NGRX

特征/商店/动作/ book.actions.ts

import { Action } from '@ngrx/store';
import { Book } from '../../models/book';

export enum BookActionTypes {
  Load = '[Book] Load',
  LoadSuccess = '[Book] Load Success',
  LoadFail = '[Book] Load Fail'
}

export class LoadBooksAction {
  readonly type = BookActionTypes.Load;
}

export class LoadBooksSuccessAction implements Action {
  readonly type = BookActionTypes.LoadSuccess;
  constructor(public payload: Book[]) {}
}

export class LoadBooksFailAction {
  readonly type = BookActionTypes.LoadFail;
}

export type BookActions
  = LoadBooksAction
  | LoadBooksSuccessAction
  | LoadBooksFailAction;

特征/商店/效应/ book.effects.ts

import { Injectable } from '@angular/core';
import { Actions, Effect } from '@ngrx/effects';
import { of } from 'rxjs';
import { catchError, map, switchMap } from 'rxjs/operators';
import { FakeApiService } from '../../services/fake-api.service';
import { LoadBooksFailAction, LoadBooksSuccessAction, BookActionTypes } from '../actions/book.actions';

@Injectable()
export class BookEffects {
    constructor(
        private fakeService: FakeApiService,
        private actions$: Actions,
    ) {}

    @Effect()
    loadBooks$ = this.actions$
      .ofType(BookActionTypes.Load)
      .pipe(
        switchMap(() => this.fakeService.getBooks()),
        map(books => (new LoadBooksSuccessAction(books))),
        catchError(error => of(new LoadBooksFailAction()))
      );
}

特征/商店/减速器/ book.reducer.ts

import { EntityState, createEntityAdapter } from '@ngrx/entity';
import { Book } from '../../models/book';
import { BookActionTypes, BookActions } from './../actions/book.actions';

export interface BooksState extends EntityState<Book> {}

export const adapter = createEntityAdapter<Book>();

const initialState: BooksState = adapter.getInitialState();

export function reducer(state = initialState, action: BookActions ): BooksState {
  switch (action.type) {
    case BookActionTypes.LoadSuccess: {
      return adapter.addAll(action.payload, state);
    }

    default: {
      return state;
    }
  }
}

特征/商店/减速器/ index.ts

import { ActionReducerMap, createFeatureSelector } from '@ngrx/store';
import * as fromOrder from './book.reducer';

export interface DemoState {
  demo: fromBook.BooksState;
}

export const reducers: ActionReducerMap<DemoState> = {
  demo: fromBook.reducer
};

export const getBooksState = createFeatureSelector<DemoState>('demo');

特征/商店/选择器/ book.selectors.ts

import * as fromBook from '../reducers/book.reducer';

export const {selectAll} = fromBook.adapter.getSelectors();

特征/ feature.module.ts

@NgModule({
  imports: [
    CommonModule,
    StoreModule.forFeature('demo', reducer),
    EffectsModule.forFeature([BookEffects])
  ],
  providers: [FakeApiService],
  declarations: []
})
export class DemoModule { }

特征/部件/书view.component.ts

@Component({
  selector: 'app-book-view',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `<book-list [books]="books$ | async"></book-list>`
})
export class BookViewComponent implements OnInit {
  books$: Observable<Book[]>;

  constructor(private store: Store<fromStore.BooksState>) {
    this.books$ = this.store.pipe(select(fromStore.selectAll));
  }

  ngOnInit() {
    this.store.dispatch(new bookActions.LoadBooksAction());
  }
}

1 个答案:

答案 0 :(得分:5)

问题在于您的选择器;您必须将book / demo状态传递到getSelectors函数

import * as fromBook from '../reducers/book.reducer';

import { getBooksState } from '../reducers';

export const selectBookState = createSelector(
  getBooksState,
  state => state.demo
);

export const {
  selectAll
} = fromBook.adapter.getSelectors(selectBookState);

查看我的回购和网址

  

https://github.com/rijine/itunes-album-angular/blob/master/src/app/album/store/selectors/albums.selector.ts