我必须遗漏一些完全明显的东西,我根本不理解错误。
错误讯息:
wwwroot/app/comments/commentList.ts(25,13): error TS2322: Type 'Comment[]' is not assignable to type 'Comment[]'.
Type 'Comment' is not assignable to type 'Comment'.
wwwroot/app/comments/commentList.ts(25,13): error TS2322: Type 'Comment[]' is not assignable to type 'Comment[]'.
Type 'Comment' is not assignable to type 'Comment'.
Property 'ID' is missing in type 'Comment'.
组件:
@Component({
selector: 'blog-comment-list',
templateUrl: './app/comments/commentList.html'
})
export class CommentList implements OnInit {
@Input() contentItemID: number;
private comments: Comment[];
constructor(private sessionService: SessionService, private blogService: BlogService) {
}
ngOnInit() {
this.blogService.GetCommentsForContentItem(this.contentItemID).subscribe(x => {
this.comments = x; // *** BUILD ERROR HERE ***
});
}
}
服务:
GetCommentsForContentItem(contentItemID: number): Observable<Comment[]>
{
let url = this.serviceURL + 'GetCommentsForContentItem?contentItemID=' + contentItemID.toString();
return this.http.get(this.noCache(url))
.map(response => this.extractData(response))
.catch(this.handleError);
}
private extractData(res: Response) {
if (res.status < 200 || res.status >= 300) {
throw new Error('Bad response status: ' + res.status);
}
let body = res.json();
return body || [];
}
private handleError(error: any) {
let errorMsg = error.message || 'Server errorx';
console.error(errorMsg);
return Observable.throw(errorMsg);
}
private noCache(url: string): string
{
if (url === null || typeof (url) === 'undefined')
return null;
if (url.slice(-1) === '/')
url = url.slice(0, -1);
let connector = url.includes('?') ? '&' : '?';
url = url + connector + 'noCache=' + (Math.random().toString().replace('.', ''));
return url;
}
型号:
export class Comment {
public ID: number;
// more properties
}
的package.json
{
"version": "1.0.0",
"name": "samsblog",
"private": true,
"dependencies": {
"@angular/common": "2.0.0-rc.1",
"@angular/compiler": "2.0.0-rc.1",
"@angular/core": "2.0.0-rc.1",
"@angular/http": "2.0.0-rc.1",
"@angular/platform-browser": "2.0.0-rc.1",
"@angular/platform-browser-dynamic": "2.0.0-rc.1",
"@angular/router": "2.0.0-rc.1",
"@angular/router-deprecated": "2.0.0-rc.1",
"@angular/upgrade": "2.0.0-rc.1",
"bootstrap": "^3.3.6",
"es6-shim": "^0.35.0",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.6",
"systemjs": "0.19.27",
"core-js": "^2.4.0",
"zone.js": "^0.6.12"
},
"devDependencies": {
"del": "2.2.0",
"gulp": "^3.9.1",
"gulp-clean": "^0.3.2",
"gulp-concat": "^2.6.0",
"gulp-ignore": "^2.0.1",
"gulp-minify": "0.0.12",
"gulp-rename": "^1.2.2",
"gulp-typescript": "^2.13.4",
"gulp-uglify": "^1.5.3",
"gulp-util": "^3.0.7",
"typescript": "^1.8.10",
"typings": "^1.0.4"
}
}
答案 0 :(得分:0)
我不完全确定,这只是猜测。
在你正在做的extractData
方法中:
return res.json() || []
其中res
为angular Response object,根据文档,json() method会返回any
。
更重要的是,你的Comment
是一个类而不是一个接口,所以你不能只拿一个json并将其作为Comment
返回,即使它们之间的属性完全匹配。 / p>
你需要这样做:
private extractData(res: Response) {
if (res.status < 200 || res.status >= 300) {
throw new Error('Bad response status: ' + res.status);
}
return (res.json() || []).map(commentJson => new Comment(commentJson));
}
interface CommentJson {
ID: number;
// more properties
}
export class Comment {
public ID: number;
// more properties
constructor(json: CommentJson) {
this.ID = json.ID;
}
}
同样,不确定这是你的问题,但是根据您发布的代码,您似乎遇到了这些问题。