如果我收到的 content-type 是 JSON-LD + Hydra,我应该使用 ngrx-data 吗?

Should I use ngrx-data, if the content-type I'm receiving is JSON-LD + Hydra?

我喜欢 JSON-LD、Hydra 的优点,并且刚刚发现 NgRx/data。

我试过在没有定制的情况下使用它,这显然是行不通的。 今天我花了几个小时在文档上,这给我留下了更多的问题。

重要的是,我不想牺牲任何关于 collection 的信息。 所以我不能只使用自定义效果器。

知道如何解决这个问题吗?

Collection 回复:

{
    "@context": "/contexts/Article",
    "@id": "/articles",
    "@type": "hydra:Collection",
    "hydra:member": [
        {
            "@id": "/articles/91",
            "@type": "http://schema.org/Article",
            "headline": "Neque illum sed adipisci dolorem rem.",
            "slug": "reiciendis-nesciunt-labore-voluptas",
            "author": {
                "@id": "/people/91",
                "@type": "http://schema.org/Person",
                "id": 91,
                "name": "Edd Nienow"
            },
            "publisher": null,
            "aggregateRating": null,
            "articleCreator": {
                "@id": "/people/91",
                "@type": "http://schema.org/Person",
                "name": "Edd Nienow"
            },
            "articleType": "tutorial",
            "creativeWorkStatus": "draft",
            "articleVersions": [
                {
                    "content": "Facere illo praesentium accusantium suscipit voluptatem laborum nihil sed. Aperiam doloremque mollitia nisi voluptatem facilis. Voluptatem sed aut et incidunt id et error modi.",
                    "articleBody": "Omnis et dolores enim praesentium. Temporibus nisi mollitia maiores voluptas nobis impedit.",
                    "brief": "Est quae minima ut ab eaque ut. Neque qui nemo quaerat consequatur.",
                    "wordCount": 779,
                    "image": null,
                    "createdAt": "1980-09-30T01:43:18+00:00"
                }
            ],
            "articleCategories": [],
            "articleTerms": [],
            "publishedAt": "2003-11-11T05:23:26+00:00",
            "createdAt": "1971-10-23T07:53:28+00:00"
        },
    ],
    "hydra:totalItems": 100,
    "hydra:view": {
        "@id": "/articles?page=4",
        "@type": "hydra:PartialCollectionView",
        "hydra:first": "/articles?page=1",
        "hydra:last": "/articles?page=4",
        "hydra:previous": "/articles?page=3"
    }

单选:

{
    "@context": "/contexts/Article",
    "@id": "/articles/91",
    "@type": "http://schema.org/Article",
    "headline": "Neque illum sed adipisci dolorem rem.",
    "slug": "reiciendis-nesciunt-labore-voluptas",
    "author": {
        "@id": "/people/91",
        "@type": "http://schema.org/Person",
        "name": "Edd Nienow"
    },
    "publisher": null,
    "aggregateRating": null,
    "articleCreator": {
        "@id": "/people/91",
        "@type": "http://schema.org/Person",
        "name": "Edd Nienow"
    },
    "articleType": "tutorial",
    "creativeWorkStatus": "draft",
    "articleVersions": [
        {
            "content": "Facere illo praesentium accusantium suscipit voluptatem laborum nihil sed. Aperiam doloremque mollitia nisi voluptatem facilis. Voluptatem sed aut et incidunt id et error modi.",
            "articleBody": "Omnis et dolores enim praesentium. Temporibus nisi mollitia maiores voluptas nobis impedit.",
            "brief": "Est quae minima ut ab eaque ut. Neque qui nemo quaerat consequatur.",
            "wordCount": 779,
            "image": null,
            "createdAt": "1980-09-30T01:43:18+00:00"
        }
    ],
    "articleCategories": [],
    "articleTerms": [],
    "publishedAt": "2003-11-11T05:23:26+00:00",
    "createdAt": "1971-10-23T07:53:28+00:00"
}

我看到了一种方法:

  1. 为您的模型添加“@”属性:
export interface HydraEntity {
    '@id': string;
    '@type': string;
}

export interface Author extends HydraEntity{
    id: number;
    name: string;
}
  1. 更改 Ngrx/data 处理 API 响应的方式,添加额外的 "hydra:*" 字段: https://ngrx.io/guide/data/entity-metadata#additionalcollectionstate

您应该能够降低您所在州的成绩。


我从未尝试过,但我认为它是在这个问题之后:https://github.com/ngrx/platform/issues/1948

导致 PR:https://github.com/ngrx/platform/pull/1921