简体   繁体   English

类型“可观察” <any> 无法使用rxjs5分配给angular2中的类型&#39;JSON&#39;

[英]Type 'Observable<any>' is not assignable to type 'JSON' in angular2 with rxjs5

I want to change all of my EventEmitter to Subjects since it is the recommended way to share variables and states instead of emitting an event. 我想将所有EventEmitter更改为Subject,因为这是共享变量和状态而不是发出事件的推荐方法。

But now I've got a problem and I just don't understand how to solve it. 但是现在我有一个问题,我只是不知道如何解决。 Here is the sample code: 这是示例代码:

@Injectable()
export class TicketsService implements OnInit {
    private _url = "http://127.0.0.1:3000/api/getAllTickets";
    private _refreshTickets:Subject<JSON> = new Subject<JSON>();
    public allTickets:JSON;

    private _url_postNewTicket = "http://127.0.0.1:3000/api/postNewTicket";    

    constructor(private _http:AuthHttp) {
        this.allTickets = this._refreshTickets
            .mergeMap(() => this._http.get(this._url))
            .map((res:Response) => res.json())
            .cache();

    }

    refreshAllTickets() {
        console.log("refresh all tickets..");
        this._refreshTickets.next(null);
    }
}

The compiler gives me the following error: 编译器给我以下错误:

public/angular/app/tickets.service.ts(26,9): error TS2322: Type 'Observable' is not assignable to type 'JSON'. public / angular / app / tickets.service.ts(26,9):错误TS2322:无法将类型“可观察”分配给类型“ JSON”。 [1] Property 'parse' is missing in type 'Observable'. [1]类型“可观察”中缺少属性“分析”。

I guess I could write allTickets:JSON as allTickets:any but in the component I use ngFor to iterate - so I get the error from ngFor that it needs an array. 我想我可以将allTickets:JSON编写为allTickets:any但是在组件中,我使用ngFor进行迭代-因此我从ngFor中得到了一个错误,它需要一个数组。

Thanks in advance! 提前致谢!

您可以将allTickets更改为Observable<any>类型,然后在ngFor绑定中使用如下所示的异步管道:

*ngFor="let ticket of allTickets | async"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular2将json observable返回类型定义为接口或类模型 - Angular2 define json observable return type to interface or class model 类型&#39;any []&#39;(JSON)上不存在Angular2属性 - Angular2 Property does not exist on type 'any[]' (JSON) 类型“AA”不可分配给类型“NgIterable”<any> | 空 | 在 Angular 中未定义 - Type 'AA' is not assignable to type 'NgIterable<any> | null | undefined' in Angular Angular2将嵌套的json对象转换为类型 - Angular2 cast a nested json object to a type 角度5,rxjs映射到json,类型“ object”上不存在“ json” - Angular 5, rxjs map to json, 'json' does not exist on the the type 'object 如何将 json 响应传递给函数:“未知”类型的参数不可分配给“any[]”类型的参数 - How to pass a json response to a function: Argument of type 'unknown' is not assignable to parameter of type 'any[]' 使用类型脚本将 Json 转换为数组 Angular2 js - Convert Json to array Angular2 js with type script 使用RxJS Observable流JSON - Stream JSON with RxJS Observable “对象”类型的参数不能分配给“JSON”类型的Httpclient GET参数 - Argument of type 'Object' is not assignable to parameter of type 'JSON' Httpclient GET &#39;{ key: string; 类型的参数 }[]&#39; 不能分配给“JSON[]”类型的参数 - Argument of type '{ key: string; }[]' is not assignable to parameter of type 'JSON[]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM