getResource
通过 ID 获取资源。
const resource = await drupal.getResource<T = JsonApiResource>( type, uuid, options?: { params, withAuth, deserialize, locale, defaultLocale, withCache, }): Promise<T>type: string- 必需
- 资源类型。例如:
node--article、taxonomy_term--tags或block_content--basic。
uuid: string- 必需
- 资源的 ID。例如:
15486935-24bf-4be7-b858-a5b2de78d09d。
options- 可选
params: JsonApiParams: JSON:API 参数,例如filter、fields、include或sort。withAuth: boolean | DrupalClientAuth:- 设置要使用的身份验证方法。请参阅 身份验证文档。
- 设置为
true以使用在客户端配置的身份验证方法。
deserialize: boolean: 设置为 false 以返回原始 JSON:API 响应。locale: string: 要获取资源的语言环境。defaultLocale: string: 网站的默认语言环境。withCache: boolean: 如果您想将资源存储并从缓存中检索,请设置withCache。cacheKey: string: 要使用的缓存键。
示例
- 通过 uuid 获取页面。
const node = await drupal.getResource( "node--page", "07464e9f-9221-4a4f-b7f2-01389408e6c8")- 通过 uuid 获取页面的
es翻译。
const node = await drupal.getResource( "node--page", "07464e9f-9221-4a4f-b7f2-01389408e6c8", { locale: "es", defaultLocale: "en", })- 获取原始 JSON:API 响应。
const { data, meta, links } = await drupal.getResource( "node--page", "07464e9f-9221-4a4f-b7f2-01389408e6c8", { deserialize: false, })- 使用缓存获取
node--article资源。
const id = "07464e9f-9221-4a4f-b7f2-01389408e6c8"
const menu = await drupal.getResource("node--article", id, { withCache: true, cacheKey: `node--article:${id}`,})TypeScript
- 对于节点实体类型,使用
DrupalNode。
import { DrupalNode } from "next-drupal"
const node = await drupal.getResource<DrupalNode>( "node--page", "07464e9f-9221-4a4f-b7f2-01389408e6c8")- 对于分类术语实体类型,使用
DrupalTaxonomyTerm。
import { DrupalTaxonomyTerm } from "next-drupal"
const term = await drupal.getResource<DrupalTaxonomyTerm>( "taxonomy_term--tags", "7b47d7cc-9b1b-4867-a909-75dc1d61dfd3")有关更多内置类型,请参阅 TypeScript 文档。