gpt4 book ai didi

angular - 类型 observable<{}> 不可分配给任何类型 :Property length is missing in type{} in Typescript

转载 作者:太空狗 更新时间:2023-10-29 17:05:15 24 4
gpt4 key购买 nike

我正在尝试使用 Angular2 http 获取保存在本地系统中的 JSON 数据。

import { Injectable }     from '@angular/core';
import { Http, Response } from '@angular/http';
import { General } from './general';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class GeneralService {
// #docregion endpoint
private statusUrl = './jsondata'; // URL to web API
// #enddocregion endpoint

// #docregion ctor
constructor (private http: Http) {}
// #enddocregion ctor

// #docregion methods, error-handling, http-get
getStatus (): Observable<General[]> {
return this.http.get(this.statusUrl)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}


private handleError (error: Response | any) {
// In a real world app, we might use a remote logging infrastructure
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}

}

我遇到错误 - Type observable<{}> 不可分配给类型“Observable”。类型“{}”不可分配给类型“General[]”。类型“{}”中缺少属性长度

这里的 General 是一个类名。我正在使用 rxjs-5.0。我正在关注 Angular.io 英雄之旅并制作我自己的项目。有什么帮助吗?

最佳答案

我改变了我的导入
从'rxjs/Observable'导入{Observable};

从 'rxjs' 导入 { Observable };
这一切都奏效了

关于angular - 类型 observable<{}> 不可分配给任何类型 :Property length is missing in type{} in Typescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40468783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com