カスタムRESTデータフェッチャーを使った辞書データの取得
このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。
RestDictionaryServiceConfig型はdataFetcherプロパティを受け入れます。このプロパティを使ってカスタムデータフェッチャーを辞書サービスのインスタンスに渡すことができます。デフォルトでは、JSS REST辞書サービスはaxiosを使用します。
!注この例は、アプリケーションのルートディレクトリにファイルを作成し、同じディレクトリにconfig.jsファイルがあることを前提としています。プロジェクトのセットアップに合わせてインポート文を調整してください。
カスタムデータフェッチャーでREST辞書サービスを使用するには:
-
dictionary-service.tsファイル内で、RestDictionaryServiceクラスのインスタンスを作成し、構成オブジェクトを提供します:
import { AxiosResponse } from 'axios'; import { AxiosDataFetcher, RestDictionaryService, DictionaryServiceData } from '@sitecore-jss/sitecore-jss'; import config from './config';
// define your custom data fetcher function dataFetcher(url: string, data?: unknown): Promise<AxiosResponse
> { return new AxiosDataFetcher().fetch(url, data); } export const dictionaryService = new RestDictionaryService({ apiHost: config.sitecoreApiHost, apiKey: config.sitecoreApiKey, siteName: config.jssAppName, // provide your custom data fetcher to the service instance dataFetcher });
-
新しい辞書サービスのインポートと利用:
import { dictionaryService } from './dictionary-service';
const language = 'en';
dictionaryService.fetchDictionaryData(language).then(data => { // do something with the data });