Ngrx /效果:获取错误TS2345:类型'Product []'的参数不能分配给'Product'类型的参数

问题描述 投票:-1回答:1

编译时,我收到以下错误(虽然浏览器控制台中没有错误,应用程序正常工作)。

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.

product.model.ts

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

product.effect

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

javascript angular typescript ngrx-store ngrx-effects
1个回答
0
投票

你定义了getProducts()来返回一个类型为Product[]的observable,但是你试图返回一个类型为Product的observable。更改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())));
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.