类型'“ shop”'的参数不能分配给类型'“ items”的参数| “大车”'

时间:2020-03-17 09:50:30

标签: node.js angular typescript ngrx

我进行了一些搜索,但似乎没有找到答案。我是Angular的新手,对于这个项目,我正在网上商店工作,我确实使用了教程,但是遇到了错误。

import { select, Store } from '@ngrx/store';
import { GetItems } from '../store/actions';
import { Product } from '../product/product.component';

  @Component({
    selector: 'app-home',
    templateUrl: './home.component.html',
    styleUrls: ['./home.component.scss']
  })
  export class HomeComponent implements OnInit {
    constructor(private store: Store<{ items: Product[]; cart: [] }>) {
      store.pipe(select('shop')).subscribe(data => (this.items = data.items));
    }

    items: Product[] = [];

    ngOnInit() {
      this.store.dispatch(new GetItems());
    }
  }

我收到的错误是:

src / app / home / home.component.ts:13:25-错误TS2769:没有重载匹配此调用。 重载1,共8个,'(mapFn:(状态:{项目:Product [];购物车:[];},道具:未知)=>未知,道具?:未知):(来源$:可观察<<项目:产品[];购物车:[];}>)=>可观察”,出现以下错误。 类型'“ shop”'的参数不能分配给类型'(state:{items:Product []; cart:[];},props:unknown)=> unknown'的参数。 重载2,共8个,'(键:“ items” |“ cart”):(源$:Observable <{项目:Product [];购物车:[];}>)=> Observable <[] | Product []>',给出以下错误。 类型'“ shop”'的参数不能分配给类型'“ items”的参数| “购物车”。

13 store.pipe(select('shop'))。subscribe(data =>(this.items = data.items)); ~~~~~~

src / app / home / home.component.ts:13:71-错误TS2339:“未知”类型上不存在属性“项目”。

13 store.pipe(select('shop'))。subscribe(data =>(this.items = data.items));

我似乎找不到问题。即使启动应用程序也给我带来了困难。有时它一口气开始,有时我需要尝试5次。但是奇怪的是,一开始,一切都会按预期进行,但是我的CMD中有错误。

我觉得我的ngrx / store出了点问题。

1 个答案:

答案 0 :(得分:0)

您需要执行以下操作以获取更具体的错误- 1)创建一个接口(AppState)并在启动商店时将其传递,这将是初始状态-

private store: Store<AppState>

2)创建一个选择器/功能选择器以选择状态的任何部分。

您可以在此处找到更多详细信息-https://ngrx.io/guide/store/selectors