我正在尝试访问效果中的当前商店状态,以便可以将当前购物车本地存储。我关注了文档,但II一直出现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[]}>
) {}
}
代码的问题在于,您不能在元组(内部switchMap
函数的函数参数)中使用类型。尝试:
switchMap(([action, storeState]: [CartActions, ProductInterface[]]) => {})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句