配置模板

常用场景的 nuxt.config.ts 配置模板,复制即用。

export default defineNuxtConfig({
  modules: ['@lacqjs/nuxt-dict'],
  dict: {
    api: { baseURL: '', dictEndpoint: '/api/dict/list' },
  },
});

GraphQL 适配器

适配器定义在独立文件中,使用 defineDictAdapter() 创建。文件放在 ~/dict/dict-adapter.ts(全局适配器约定路径),模块自动发现。

// 全局 GraphQL 适配器——替代默认 REST 请求
export default defineDictAdapter({
  async fetchDict(storeName, { types, locale }) {
    // 通过 GraphQL 查询字典数据
    const res = await fetch('https://gql.example.com', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        query: `{ dict(types: [${types.map((t) => `"${t}"`)}], locale: "${locale}") { version data { type items { code label } } } }`,
      }),
    });
    return (await res.json()).data.dict;
  },
  async fetchVersion(storeName) {
    // 通过 GraphQL 查询版本号
    const res = await fetch('https://gql.example.com', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ query: '{ dictVersion }' }),
    });
    return (await res.json()).data.dictVersion;
  },
});

仓库自定义适配器

每个仓库可以使用独立的适配器文件。约定路径 ~/dict/{storeName}-adapter.ts,模块自动发现。

// static 仓库的自定义适配器——直接返回硬编码数据,不发起 HTTP 请求
export default defineDictAdapter({
  async fetchDict(_storeName, { types, locale }) {
    return {
      data: {
        priority: {
          type: 'priority',
          items: [
            { value: 'high', label: `高 (${locale})` },
            { value: 'low', label: `低 (${locale})` },
          ],
        },
      },
    };
  },
  async fetchVersion(_storeName) {
    return 'static-1.0';
  },
});