Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何在可观察对象上设置正确的类型?_Angularjs_Typescript_Rxjs - Fatal编程技术网

Angularjs 如何在可观察对象上设置正确的类型?

Angularjs 如何在可观察对象上设置正确的类型?,angularjs,typescript,rxjs,Angularjs,Typescript,Rxjs,新的观测/角度2。在我的组件上正确更新了内容,但我从typescript中得到以下错误: 类型为“StoreItem[]”的参数不能分配给类型为的参数 “字符串” 有人能给我一些关于如何设置正确类型的指导吗 //store.item.interface.ts export interface StoreItem { id?: number; qty?: number; nameList?:Array<string>; title: string; price:

新的观测/角度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)

  }
}
//store.item.interface.ts
导出接口存储项{
id?:编号;
数量:数量;
名称列表?:数组;
标题:字符串;
价格:数量;
}
//cart.service.ts
从“@angular/core”导入{Injectable}
从'rxjs/Subject'导入{Subject};
从“../store.item.interface”导入{StoreItem}
@可注射()
出口级货运服务{
private_subject=新主题();
private _cartList:StoreItem[]=[];
cartList$=此._subject.asObservable();
附加项(项目:StoreItem){
此._cartList.push(项目)
此.\u主题.下一个(此.\u目录)
}
}

只需为主题设置正确的类型(假设您希望发出整个
\u cartList
数组):

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