我的代码可以工作,但似乎有点脏。 有办法改进它吗?
private AppointmentUrlGet = 'http://localhost:8080/datas'
.
.
.
getDataByYear(): Promise<UserData[]> {
var body ="?fromDate=01-01-" + this.yearOfToday +"&toDate=31-12-
" + this.yearOfToday +"&limit=100";
var urlByYear = this.AppointmentUrlGet+body
return this.http.get(urlByYear)
.toPromise()
.then(response => this.parseUserData(response))
.catch(this.handleError);
}
感谢您的帮助
更多关于单一责任:
urlByYear(): string {
var body = "?fromDate=01-01-" + this.yearOfToday +"&toDate=31-12-" + this.yearOfToday +"&limit=100";
return this.AppointmentUrlGet + createBody();
}
getDataByYear(): Promise<UserData[]> {
return this.http.get( urlByYear() )
.toPromise()
.then(response => this.parseUserData(response))
.catch(this.handleError);
}
我更喜欢保持我的班级小。因此,将URL方法放在其职责生成URL或类似内容的不同类中。
然后将其作为依赖项注入到负责HTTP的服务或类中。
阅读固体原则。