我有一个模型类Program,并创建了一个名为github.service.ts的服务类。当我创建Program类型数组的变量并尝试推送元素(一个Program对象)时,它给出了编译错误,提示“类型[{}”上不存在[ts]属性'push'。” >
program.model.ts
export class Program{
constructor(public name: string, public url: string){};
}
github.service.ts
import { Program } from 'src/app/learning/program.model';
import { Injectable} from '@angular/core';
import { Http , Response} from '@angular/http';
import 'rxjs/Rx';
@Injectable()
export class GithubService{
private programList: Program[] = [new Program('test','test')];
constructor(private http: Http){}
getPracticeQuestions(){
return this.http.get('https://api.github.com/repos/testuser/practice/contents/')
.map(
(response: Response) => {
const programs: Program[] = response.json();
for( let program of programs){
const res: Program = new Program(program.name, program.url);
console.log('res:'+res.name+"::"+res.url);
this.programList.push(res);
}
}
);
}
}
programList类型显示为:(属性)GithubService.programList:{}
答案 0 :(得分:0)
尝试一下:
private programList = new Array<Program>();
答案 1 :(得分:0)
答案 2 :(得分:0)
答案 3 :(得分:-1)
programList:Array = [new Program('test','test')];