2016-09-26 52 views
4

我有一個HATEOAS鏈接支持的寧靜Web服務。當我撥打 「http://localhost:8080/v1/bookings/1225380?lock=true」 鏈接時,我獲得了以下資源URL。我想將這些超媒體與我的Angular2應用程序(最近升級到最終版本)相集成。我發現在Angular1的支持下,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的資源。使用HATEOAS支持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" 
    } 
] 

回答

1

您可以爲此創建一個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) 

希望這有助於:)

相關問題