Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 无法读取属性';getSubjectifs';未定义的_Angular - Fatal编程技术网

Angular 无法读取属性';getSubjectifs';未定义的

Angular 无法读取属性';getSubjectifs';未定义的,angular,Angular,我是angular 4的新手,我有一个任务,我应该从Json文件中获取一个名称列表,这是我的AppService,我在这里获取Json文件 import { Injectable } from '@angular/core'; import { Http, Headers, Response } from '@angular/http'; import { Observable } from 'rxjs'; import 'rxjs/add/operator/map' @Injectable(

我是angular 4的新手,我有一个任务,我应该从Json文件中获取一个名称列表,这是我的AppService,我在这里获取Json文件

import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map'

@Injectable()

export class AppServices {


private _url: string = "assets/data/queryoptions.json";

constructor(private http: Http) {

}

getSubjectifs() {
    return this.http.get(this._url)
        .map((response: Response) => response.json());
}

}
这是我的组件

ngOnInit(): void {


    this.form = this._fb.group({
        items: new FormArray([])
    });
    this.refrech();

}
refrech(): void {
    this.appservice.getSubjectifs().subscribe(ressubjectifsdata => this.subjectifs = ressubjectifsdata);
}



So any idea why i'm getting Cannot read property 'getSubjectifs' of undefined ? 

您必须在组件中注入服务:

constructor(private appservice: AppServices ) {

}       
refrech(): void {
     this.appservice.getSubjectifs().subscribe(ressubjectifsdata => this.subjectifs = ressubjectifsdata);
}

您是否在构造函数中插入了
appservice
?如果插入,请检查命名,可能是这样。appservice是的,问题出在构造函数中,我只需要将私有appservice:AppServices添加到我的构造函数中,并且我需要添加“.subjectifs”以使其正常工作。appservice.getSubjectifs().subscribe(ressubjectifsdata=>this.subjectifs=ressubjectifsdata.subjectifs);