javascript - Ngrx/效果 : get error TS2345: Argument of type 'Product[]' is not assignable to parameter of type 'Product'

标签 javascript angular typescript ngrx-store ngrx-effects

编译时,出现以下错误(尽管浏览器控制台中没有错误,并且应用程序工作正常)。

ERROR in src/app/services/product.service.ts(15,9): error TS2322: Type 'Observable<Product>' is not assignable to type 'Observable<Product[]>'.
  Type 'Product' is not assignable to type 'Product[]'.
src/app/store/effects/product.effect.ts(22,76): error TS2345: Argument of type 'Product[]' is not assignable to parameter of type 'Product'.
  Type 'Product[]' is missing the following properties from type 'Product': id, name, description
src/app/store/effects/product.effect.ts(23,44): error TS2554: Expected 0 arguments, but got 1.
src/app/store/reducers/index.ts(9,5): error TS2322: Type '(state: State, action: Action) => State | { loading: boolean; loaded: boolean; products: Product; }'
is not assignable to type 'ActionReducer<State, Action>'.
  Type 'State | { loading: boolean; loaded: boolean; products: Product; }' is not assignable to type 'State'.
    Type '{ loading: boolean; loaded: boolean; products: Product; }' is not assignable to type 'State'.
      Types of property 'products' are incompatible.
        Type 'Product' is missing the following properties from type 'Product[]': length, pop, push, concat, and 26 more.

产品.model.ts

export interface Product {
    id: number;
    name: string;
    description: string;
}

产品效果

import { Effect, Actions, ofType } from '@ngrx/effects';
import { map, switchMap, catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { Injectable } from '@angular/core';

import * as productActions from '../actions';
import * as fromServices from '../../services';

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

    @Effect()
    loadProducts$ = this.actions$.pipe(ofType(productActions.LOAD_PRODUCTS),
        switchMap(() => {
            return this.productService
                .getProducts()
                .pipe(
                    map(products => new productActions.LoadProductsSuccess(products)),
                    catchError(error => of(new productActions.LoadProductsFail(error)))
                );
        })
    );
}

状态截图

screen

最佳答案

您定义了 getProducts() 以返回 Product[] 类型的可观察量,但您却试图返回 Product 类型的可观察量。更改 getProducts() 以返回 any 类型的可观察对象

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';
import { catchError } from 'rxjs/operators';
import 'rxjs/add/observable/throw';

import { Product } from '../models';

@Injectable({providedIn: 'root'})
export class ProductService {
    constructor(private http: HttpClient) {}

    getProducts(): Observable<any> {
        return this.http
            .get(`/assets/products.json`)
            .pipe(catchError((error: any) => Observable.throw(error.json())));
    }

}

关于javascript - Ngrx/效果 : get error TS2345: Argument of type 'Product[]' is not assignable to parameter of type 'Product' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54948784/

相关文章:

javascript - 基于一个数组对多个数组进行排序

javascript - 如何在适用于 Android 平台的 PhoneGap 应用程序中接收即时通知,告知您收件箱中有来自管理员的新消息

javascript - 通过 Angular 移动 DOM 元素时保持焦点

angular2 无法导入 ng2-bootstrap

angular - Angular2(TypeScript)中 ${} 符号的名称是什么

typescript - Jest typescript 测试运行两次,一次用于 ts 文件,一次用于 js 文件

javascript - 我的 js 脚本会因在旧版浏览器上设置 css --variables 而中断吗?

javascript - d3 将鼠标悬停在表格上时为图表制作动画

html - 将div并排放置在列中

javascript - 检查接口(interface)是否有属性?