如何在Observables上设置正确的类型?

时间:2016-10-30 19:39:06

标签: angularjs typescript rxjs

observables / angular的新功能2.在我的组件上正确更新了东西,但是我从typescript中得到了这个错误:

  

类型'StoreItem []'的参数不能分配给类型的参数   '字符串'

有人可以就如何设置正确的类型给我一些指导吗?

//store.item.interface.ts
  export interface StoreItem {
  id?: number;
  qty?: number;
  nameList?:Array<string>;
  title: string;
  price: number;
}


//cart.service.ts

import {Injectable} from '@angular/core'
import {Subject}    from 'rxjs/Subject';
import { StoreItem } from '../store.item.interface'

@Injectable()
export class CartService {
  private _subject = new Subject<string>();
  private _cartList:StoreItem[] = [];

  cartList$ = this._subject.asObservable();


  addItem(item:StoreItem){
    this._cartList.push(item)
    this._subject.next(this._cartList)

  }
}

1 个答案:

答案 0 :(得分:-1)

只需为Subject设置正确的类型(假设您要发出整个_cartList数组):

private _subject = new Subject<StoreItem[]>();