无法在NGRX效果元组错误中获取状态

时间:2020-02-11 18:41:00

标签: angular ngrx

我正在尝试访问效果中的当前商店状态,以便可以将当前购物车本地存储。我遵循了文档,但是我一直遇到TS错误。以下是我的影响及其引发的错误。我不确定是什么原因导致此问题,因为我不熟悉该错误。

src / app / cart / store / cart.effects.ts(15,42)中的

错误:错误TS2493:长度为'2'的元组类型'[never,ProductInterface []]'具有索引“ 2”处没有元素。 [ng] src / app / cart / store / cart.effects.ts(15,54):错误TS2493:长度为2的元组类型'[never,ProductInterface []]'在索引'3'处没有元素。

import {Injectable} from '@angular/core';
import {Actions, createEffect, ofType} from '@ngrx/effects';
import {Store, select} from '@ngrx/store';
import {ProductInterface} from '../../interfaces/product.interface';
import {of} from 'rxjs'
import {CartActions, addToCart, clearCart, removeFromCart, updateCartQuantity, } from './cart.actions';
import {withLatestFrom, switchMap} from 'rxjs/operators';
import {Storage} from '@ionic/storage';
@Injectable()
export class CartEffects {

    storeCart$ = createEffect(() => this.actions$.pipe(
        ofType('[Cart Component] Add To Cart'),
        withLatestFrom(this.store.pipe(select('cart'))),
        switchMap(([action: CartActions, storeState: ProductInterface[]]) => {
            this.storage.set('cart', storeState);
            return of(action)
        })))

    constructor(
        private actions$: Actions,
        private storage: Storage,
        private store: Store<{cart: ProductInterface[]}>
    ) {}
}

1 个答案:

答案 0 :(得分:2)

您的代码存在问题,即您不能在元组(内部switchMap函数的函数参数)中使用类型。试试:

switchMap(([action, storeState]: [CartActions, ProductInterface[]]) => {})
相关问题