HATEOAS 的 Angular2 支持

2023-12-22

在 HATEOAS 链接的支持下,我拥有一个轻松的网络服务。当我打电话时 ”http://localhost:8080/v1/bookings/1225380?lock=true 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://paulcwarren.wordpress.com/2015/04/03/role-based-spas-with-angularjs-and-spring-hateoas/, https://github.com/LuvDaSun/angular-hal/tree/master/src 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);
}

}

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

constructor(private hypermedia:Hypermedia)

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

this.hypermedia.get(myHypermediaLinksArray,myRel)

希望这可以帮助:)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HATEOAS 的 Angular2 支持 的相关文章

随机推荐