简体   繁体   English

为什么我的@Effect用Angular 6中的ngrx覆盖现有状态?

[英]Why is my @Effect overwriting existing state with ngrx in Angular 6?

I am using ngrx in an Angular 6 app and I can successfully load data from a database and display it on the screen. 我在Angular 6应用程序中使用ngrx,我可以成功地从数据库加载数据并将其显示在屏幕上。 However, when I navigate to a new page in the app and then navigate back, the state is not preserved and it appears the @Effect is called again and loads the data from the database again. 但是,当我导航到应用程序中的新页面然后返回时,状态不会保留,并且似乎再次调用@Effect并再次从数据库加载数据。 Here is my code: 这是我的代码:

Effect 影响

export class ProductEffects {
    constructor(private productService: ProductService, private actions$: Actions) { }

    @Effect()
    loadProducts$: Observable<Action> = this.actions$.pipe(
        ofType(productActions.LOAD_ACTION),
        switchMap(action =>
            this.productsService.getProductDetails().pipe(
                map(product => new productActions.LoadSuccess(product)),
                catchError(err => of(new productActions.LoadError(err)))
            )
        )
    );

Reducer 减速器

export interface ProductState {
    product: Product;
}

const initialState: ProductState = {
    product: {}
};

export function reducer(state = initialState, action: Action) {
    switch (action.type) {
        case SET_PRODUCT:
            return { ...state, product: (action as SetProductAction).payload };

        case LOAD_SUCCESS:
            return { ...state, product: (action as LoadSuccess).payload, error: '' };

        default: return state;
    }
}

Actions 操作

export const SET_PRODUCT = '[Product] Set Product';
export const LOAD = '[Product] Load';
export const LOAD_SUCCESS = '[Product] Load Success';
export const LOAD_ERROR = '[Product] Load Error';

export class SetProductAction implements Action {
    readonly type = SET_PRODUCT;
    constructor(public payload: Product) { }
}

export class Load implements Action {
    readonly type = LOAD;
}

export class LoadSuccess implements Action {
    readonly type = LOAD_SUCCESS;
    constructor(public payload: Product) { }
}

export type ProductActions = SetProduct | Load | LoadSuccess;

Store 商店

export interface State extends fromRoot.AppState {
    product: fromProduct.ProductState;
}

// selectors
const getProductState = createFeatureSelector<fromProduct.ProductState>('product');

export const getProduct = createSelector(
    getProductState,
    state => state.product
}

Component 零件

products$: Observable<Product>;

constructor(private store: Store<fromProduct.State>) { }

ngOnInit() {
    this.store.dispatch(new Load());
    this.products$ = this.store.pipe(select(fromProduct.getProduct));
}

Your ngrx implementation seems all ok to me. 您的ngrx实施对我来说似乎还可以。 What you describe is an absolutely normal behavior. 您描述的是绝对正常的行为。 As soon as you navigate away from a page, the component destroyed and the new one is created, ie the ngOnInit is called. 离开页面后,该组件将被破坏并创建一个新组件,即调用ngOnInit。 If you put the Loading logic in the ngOnInit, the state is loaded everytime you navigate to this page. 如果将“加载”逻辑放在ngOnInit中,则每次导航到此页面时都会加载状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM