类型“ {}”上不存在属性“ push”

时间:2019-06-22 04:58:46

标签: angular typescript

我有一个模型类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:{}

4 个答案:

答案 0 :(得分:0)

尝试一下:

 private programList = new Array<Program>();

答案 1 :(得分:0)

您需要重新检查完整的源代码,将其设置为其他对象{}

我尝试复制您的源代码,它正常工作。

https://stackblitz.com/edit/angular-ximbqd

答案 2 :(得分:0)

代码很好,在某些响应中可能是错误的,

请参阅

https://stackblitz.com/edit/angular-addinarray

答案 3 :(得分:-1)

programList:Array = [new Program('test','test')];