我在這里有點迷茫,我想使用 angular 將當前日期插入到 api url 中。這是我到目前為止所做的。
這是我的 Ts
import { HttpClient} from '@angular/common/http';
import { DatePipe } from '@angular/common';
@Component({
selector: 'app-matches',
templateUrl: './matches.component.html',
styleUrls: ['./matches.component.css']
})
export class MatchesComponent implements OnInit {
public myMatches: any = []
myDate = new Date();
constructor( private http: HttpClient , public datePipe: DatePipe) {
let myDates = this.datePipe.transform(this.myDate, 'yyyy-MM-dd');
}
getMyMatches(){
const url = "https://app.sportdataapi.com/api/v1/soccer/matches?apikey=myKey&season_id=1980&date_from= myDates"
return this.http.get(url).subscribe((res)=>{
this.myMatches =res
console.log(res);
})
}
ngOnInit(): void {
this.getMyMatches()
}
}```
In my browser console i get 500 response status. DatePipe has been added in app module's providers .
uj5u.com熱心網友回復:
錯誤不在 DatePipe 中。
@Component({
selector: 'app-matches',
templateUrl: './matches.component.html',
styleUrls: ['./matches.component.css']
})
export class MatchesComponent implements OnInit {
public myMatches: any = []
myDates: any;
constructor( private http: HttpClient , public datePipe: DatePipe) {
this.myDates = this.datePipe.transform(new Date(), 'yyyy-MM-dd');
}
getMyMatches(){
const url = "https://app.sportdataapi.com/api/v1/soccer/matches?apikey=myKey&season_id=1980&date_from=" this.myDates;
return this.http.get(url).subscribe((res)=>{
this.myMatches =res
console.log(res);
})
}
ngOnInit(): void {
this.getMyMatches()
}
}
uj5u.com熱心網友回復:
您必須使用字串插值。試試這個
const url = `https://app.sportdataapi.com/api/v1/soccer/matches?apikey=myKey&season_id=1980&date_from=${ myDates}`
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/390102.html
上一篇:我如何通過更新嵌套值。API?
