Angular2支持HATEOAS

前端之家收集整理的这篇文章主要介绍了Angular2支持HATEOAS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在HATEOAS链接支持下,我有一个安静的Web服务.我打电话的时候
http://localhost:8080/v1/bookings/1225380?lock=true
链接我得到了以下资源URL.我想将这些Hypermedia与我的Angular2应用程序(最近升级到最终版本)集成.我发现Angular1在Angular HAL的支持下实现的资源很少(链接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,并使用此类而不是角度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);
}

}

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

constructor(private hypermedia:Hypermedia)

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

this.hypermedia.get(myHypermediaLinksArray,myRel)

希望这可以帮助:)

猜你在找的Angularjs相关文章