Angular HTTPGET与使用URLEncoded的api一起使用

Angular HTTPGET与使用URLEncoded的api一起使用,angular,http,get,Angular,Http,Get,我的代码正在运行,但似乎有点脏。有什么办法可以改进它吗 private AppointmentUrlGet = 'http://localhost:8080/datas' . . . getDataByYear(): Promise<UserData[]> { var body ="?fromDate=01-01-" + this.yearOfToday +"&toDate=31-12- " + this.yearOfToday +"

我的代码正在运行,但似乎有点脏。有什么办法可以改进它吗

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);

          }
private AppointmentUrlGet='1〕http://localhost:8080/datas'
.
.
.
getDataByEAR():承诺{
var body=“?fromDate=01-01-”+this.yearOfToday+”&toDate=31-12-
“+this.yearOfToday+”&limit=100”;
var urlByYear=this.AppointmentUrlGet+body
返回此.http.get(urlByYear)
.toPromise()
.then(response=>this.parseUserData(response))
.接住(这个.把手错误);
}

感谢您的帮助

更多关于单一责任:

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);

      }
urlByYear():字符串{
var body=“?fromDate=01-01-”+this.yearOfToday+”&toDate=31-12-“+this.yearOfToday+”&limit=100”;
返回this.AppointmentUrlGet+createBody();
}
getDataByEAR():承诺{
返回此.http.get(urlByYear())
.toPromise()
.then(response=>this.parseUserData(response))
.接住(这个.把手错误);
}
我更喜欢小班上课。所以把URL方法放在一个不同的类中,它负责生成URL或类似的东西

然后将其作为依赖项注入负责HTTP的服务或类中

了解一下原则