我是Angular 2和打字稿的新手。Angular typingcript
这是我的跟踪组件,我从API获取跟踪。
import { Component, ViewEncapsulation, OnInit, OnDestroy } from
'@angular/core';
import { ActivatedRoute } from '@angular/router';
import * as moment from 'moment';
import { TrackService } from './track.service';
import { Track } from './track.model';
import { Response } from '@angular/http';
@Component({
selector: 'da-article',
templateUrl: './track.html',
styleUrls: ['./track.scss'],
encapsulation: ViewEncapsulation.Emulated,
providers: [TrackService]
})
export class TrackComponent implements OnInit, OnDestroy {
private track: Track;
private error: Response;
private isLoading: boolean = true;
private releasedTrack: string = moment("2013-03-10T02:00:00Z").format('DD-MM-YYYY');
private duration: string = moment(345678).format("m:ss");
constructor(
private route: ActivatedRoute,
private trackService: TrackService
) {
}
ngOnInit(): void {
let TrackId = this.route.snapshot.params['trackId'];
this.trackService.getAll().subscribe(
(data) => this.track = data[TrackId],
(error) => this.error = error,
() => this.isLoading = false
);
}
ngOnDestroy(): void {}
}
在这些线
私人releasedTrack:字符串=时刻。( “2013-03-10T02:00:00Z”)格式( 'DD-MM-YYYY'); private duration:string = moment(345678).format(“m:ss”);
我想通过{this.track}而不是硬编码的值。
This.track是不确定的,(因为它是不可用)
我怎样才能做到这一点?
由于
的确,'this.track'在对象构建时不可用。当'this.track'变为可用时,您必须在'ngOnInit'中稍后执行。 – deceze