\n\n\n```\n\n \n \n\nTo utilize conditional content in Svelte components, retrieve it via the `useIntlayer` hook. The store is accessed with `$`. Here's an example:\n\n```svelte fileName=\"**/*.svelte\"\n\n\n
\n

{$content.myCondition(true)}

\n

{$content.myCondition(false)}

\n
\n```\n\n
\n \n\nTo utilize conditional content in Preact components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\nimport type { FC } from \"preact\";\nimport { useIntlayer } from \"preact-intlayer\";\n\nconst ConditionalComponent: FC = () => {\n const { myCondition } = useIntlayer(\"my_key\");\n\n return (\n
\n

{myCondition(true)}

\n

{myCondition(false)}

\n
\n );\n};\n\nexport default ConditionalComponent;\n```\n\n
\n \n\nTo utilize conditional content in SolidJS components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\nimport type { Component } from \"solid-js\";\nimport { useIntlayer } from \"solid-intlayer\";\n\nconst ConditionalComponent: Component = () => {\n const { myCondition } = useIntlayer(\"my_key\");\n\n return (\n
\n

{myCondition(true)}

\n

{myCondition(false)}

\n
\n );\n};\n\nexport default ConditionalComponent;\n```\n\n
\n \n\nTo utilize conditional content in Angular components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```typescript fileName=\"app.component.ts\" codeFormat=\"typescript\"\nimport { Component } from \"@angular/core\";\nimport { useIntlayer } from \"angular-intlayer\";\n\n@Component({\n selector: \"app-conditional\",\n template: `\n
\n

{{ content().myCondition(true) }}

\n

{{ content().myCondition(false) }}

\n
\n `,\n})\nexport class ConditionalComponent {\n content = useIntlayer(\"my_key\");\n}\n```\n\n
\n \n\nTo utilize conditional content with `vanilla-intlayer`, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```typescript fileName=\"**/*.ts\" codeFormat={[\"typescript\", \"esm\"]}\nimport { installIntlayer, useIntlayer } from \"vanilla-intlayer\";\n\ninstallIntlayer();\n\nconst content = useIntlayer(\"my_key\").onChange((newContent) => {\n document.getElementById(\"true-content\")!.textContent =\n newContent.myCondition(true);\n document.getElementById(\"false-content\")!.textContent =\n newContent.myCondition(false);\n});\n\n// Initial render\ndocument.getElementById(\"true-content\")!.textContent =\n content.myCondition(true);\ndocument.getElementById(\"false-content\")!.textContent =\n content.myCondition(false);\n```\n\n \n\n\n## 其他资源\n\n有关配置和使用的更多详细信息,请参考以下资源:\n\n- [Intlayer CLI 文档](/zh/doc/concept/cli)\n- [React Intlayer 文档](/zh/doc/environment/create-react-app)\n- [Next Intlayer 文档](/zh/doc/environment/nextjs/15)\n\n这些资源提供了在各种环境和框架中设置和使用 Intlayer 的进一步见解。\n","description":"了解如何在Intlayer中使用条件内容,以根据特定条件动态显示内容。按照本文档的步骤高效实现条件。","url":"https://intlayer.org/zh/doc/concept/content/condition","datePublished":"2025-02-07","dateModified":"2025-06-29","keywords":"条件内容, 动态渲染, 文档, Intlayer, Next.js, JavaScript, React","license":"https://raw.githubusercontent.com/aymericzip/intlayer/refs/heads/main/LICENSE","audience":{"@type":"Audience","audienceType":"开发者,内容经理"}}
    作者:
    Creation:2025-02-07Last update:2025-06-29

    条件内容 / Intlayer 中的条件

    条件如何工作

    在 Intlayer 中,通过 cond 函数实现条件内容,该函数将特定条件(通常是布尔值)映射到其对应的内容。这种方法使您能够根据给定条件动态选择内容。当与 React Intlayer 或 Next Intlayer 集成时,会根据运行时提供的条件自动选择适当的内容。

    设置条件内容

    要在您的 Intlayer 项目中设置条件内容,请创建一个包含条件定义的内容模块。以下是各种格式的示例。

    **/*.content.ts
    import { cond, type Dictionary } from "intlayer";
    
    const myConditionalContent = {
      key: "my_key",
      content: {
        myCondition: cond({
          true: "当条件为真时的内容",
          false: "当条件为假时的内容",
          fallback: "当条件不满足时的内容", // 可选
        }),
      },
    } satisfies Dictionary;
    
    export default myConditionalContent;
    如果未声明 fallback,当条件不满足时将使用最后声明的键作为 fallback。

    在 React Intlayer 中使用条件内容

    To utilize conditional content within a React component, import and use the useIntlayer hook from the react-intlayer package. This hook fetches the content for the specified key and allows you to pass in a condition to select the appropriate output.

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const ConditionalComponent: FC = () => {
    const { myCondition } = useIntlayer("my_key");
    
    return (
      <div>
        <p>
          {
            /* Output: my content when it's true */
            myCondition(true)
          }
        </p>
        <p>
          {
            /* Output: my content when it's false */
            myCondition(false)
          }
        </p>
        <p>
          {
            /* Output: my content when the condition fails */
            myCondition("")
          }
        </p>
        <p>
          {
            /* Output: my content when the condition fails */
            myCondition(undefined)
          }
        </p>
      </div>
    );
    };
    
    export default ConditionalComponent;

    其他资源

    有关配置和使用的更多详细信息,请参考以下资源:

    这些资源提供了在各种环境和框架中设置和使用 Intlayer 的进一步见解。