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)
}
}
答案 0 :(得分:-1)
只需为Subject设置正确的类型(假设您要发出整个_cartList
数组):
private _subject = new Subject<StoreItem[]>();