2017-03-15 2 views
0

ausgeführt Wenn ich meinen Dienst im Konstruktor aufgerufen, bekomme ich undefiniert. der Service wird in ngOnInit Methode aufgerufen, Von Difference between Constructor and ngOnInit Ich habe gesehen, dass Konstruktor zuerst ausgeführt, aber in meinem Fall bemerkte ich das Gegenteil, also bin ich etwas verwirrt. habe jemand mehr Erklärung darüber, danke.ngOnInit und Konstruktor wer zuerst

constructor(private curveService :ProgressCurveService, private util : UtilService) { 
    this.startPickerOptions = new DatePickerOptions(); 
    this.endPickerOptions = new DatePickerOptions(); 
    //this.datePickerOptions.initialDate = new Date(Date.now()); 
    } 

ngOnInit() { 
    this.curveService.instance.getCurve(this.startDate.formatted,this.endDate.formatted,this.amplutid). 
    then(res => { 

     this.lineChartLabels = this.util.dateToShortString(Object.keys(res.progressPlotData)) 
     this.lineChartData = this.util.objectToIntArray(res.progressPlotData); 
    }).catch(res => console.log('error if date selecting ...')); 
} 

Verlaufskurve Service:

import { progressCurveItf } from './progress-curve/progress-curve-interface'; 

@Injectable() 
export class ProgressCurveService { 

    state : string = 'project'; 
    constructor(private prsCurve : PrsProgressCurveService, private projCurve : ProjProgressCurveService) { } 
    get instance():progressCurveItf{ 

    if(this.state == 'subproject'){ 
     return this.prsCurve; 
    } else { 
     return this.projCurve; 
    } 
    } 

} 
+0

Der Konstruktor wird zuerst ausgeführt. Das Problem muss von woanders kommen. –

+0

genau, bereits in der Dokumentation auch erläutert, dass der Konstruktor zuerst ausgeführt –

+0

Ich bekomme kann nicht lesen Eigenschaftsinstanz von undefiniert Ausnahme – mustafa918

Antwort

0

Während Sie eine Instanz vom Typ progressCurveItf sind Rückkehr, die eine Schnittstelle I denke, es ist etwas falsch mit der Instanziierung, was Sie Rückkehr Scheck wenn Sie PrsProgressCurveService und ProjProgressCurveService angeben.

Verwandte Themen