HATEOAS 对 Angular2 的支持

标签 angular hateoas

我有一个支持 HATEOAS 链接的 Restful 网络服务。当我打电话 “http://localhost:8080/v1/bookings/1225380?lock=true” 链接 我得到了以下资源 URL。我想将这些超媒体与我的 Angular2 应用程序(最近升级到最终版本)集成。我发现很少有资源是在 Angular HAL 的支持下使用 Angular1 实现的(链接 - https://paulcwarren.wordpress.com/2015/04/03/role-based-spas-with-angularjs-and-spring-hateoas/https://github.com/LuvDaSun/angular-hal/tree/master/src)。但是我找不到 Angular2 的资源。

"links": [
  {
    "rel": "client",
    "href": "http://localhost:8080/v1/clients/10000"
  },
  {
    "rel": "passengers",
    "href": "http://localhost:8080/v1/bookings/1225380/passengers"
  },
  {
    "rel": "itinerary",
    "href": "http://localhost:8080/v1/bookings/1225380/itinerary"
  },
  {
    "rel": "self",
    "href": "http://localhost:8080/v1/bookings/1225380?lock=true"
  },
  {
    "rel": "clientBookingHistory",
    "href": "http://localhost:8080/v1/bookings/1225380/clientBookingHistory/10000"
  }
]

最佳答案

您可以为此创建一个 Injectable 并使用此类而不是 Angular http 类。在这里您过滤链接,然后使用正确的链接调用 http。

@Injectable() 
export class Hypermedia {

   constructor(private http: Http) { }

   get(links: any[], rel: String, body?: any, options?: RequestOptionsArgs): Observable<Response> {
    var link = null;
    var request = null;

    // Find the right link
    links.forEach(function (_link) {
        if (_link.rel === rel) {
            link = _link
            return;
        }
    });

    return this.http.get(link.href);
}

提供这个注入(inject)器并把它添加到你需要的构造函数中

constructor(private hypermedia:Hypermedia)

然后你可以简单地调用它,就像你通常调用 http 类一样

this.hypermedia.get(myHypermediaLinksArray,myRel)

希望这有帮助:)

关于HATEOAS 对 Angular2 的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39696760/

相关文章:

Angular2 与 modernizr

Spring 数据休息 - _links

angular - Typescript中的字符串插值,用变量替换 'placeholders'

angular - Angular 5 中日期时间选择器中的回调方法

angular - ngClass 最初不应用 css 类,仅在单击后应用

grails - 如何创建 Grails HAL 转换器

c# - 在 Web API 中生成超媒体链接

html - 如何在使用图像映射创建的特定区域添加图钉

.net - PowerShell URI 模板解析

REST API设计: Where to create Resource that depends from other resource?