HATEOAS对Angular2的支持 [英] Angular2 support with HATEOAS

查看:87
本文介绍了HATEOAS对Angular2的支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HATEOAS链接的支持下,我有一个宁静的Web服务.当我打电话 " http://localhost:8080/v1/bookings/1225380?lock = true 链接我得到了以下资源URL.我想将这些Hypermedia与我的Angular2应用程序集成(最近升级到最终版本).我发现在Angular HAL的支持下使用Angular1实现的资源很少(链接- https://github.com/LuvDaSun/angular-hal/tree/master/src ).但是我找不到Angular2的资源.

I have a restful web service with the support of HATEOAS links. When I call "http://localhost:8080/v1/bookings/1225380?lock=true" link I got following resource URLs. I want to integrate these Hypermedia with my Angular2 application (recently upgraded to final version). I found few resources which were implemented with Angular1 with the support of Angular HAL (links - https://paulcwarren.wordpress.com/2015/04/03/role-based-spas-with-angularjs-and-spring-hateoas/, https://github.com/LuvDaSun/angular-hal/tree/master/src). But I am unable to found a resource for 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并使用该类而不是有角的http类.在这里,您可以过滤链接,然后使用正确的链接调用http.

You can create an Injectable for this and use this class instead of the angular http class. Here you filter the links and than call http with the right link.

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

}

提供此注入器并将其添加到您需要的构造器中

Provide this injector and add it to the constructor where you need it

constructor(private hypermedia:Hypermedia)

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

Then you can simply call it like you normally would call the http class

this.hypermedia.get(myHypermediaLinksArray,myRel)

希望这会有所帮助:)

这篇关于HATEOAS对Angular2的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆