Creation:2025-02-07Last update:2025-06-29
将此文档参考到您的 AI 助手ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
此页面的内容已使用 AI 翻译。
查看英文原文的最新版本Edit this doc
If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy
Copy doc Markdown to clipboard
嵌套 / 子内容引用
嵌套如何工作
在 Intlayer 中,嵌套是通过 nest 函数实现的,它允许您引用和重用来自另一个字典的内容。与其重复内容,不如通过其键指向现有的内容模块。
设置嵌套
To use nested content in a React component, leverage the useIntlayer hook from the react-intlayer package. This hook retrieves the correct content based on the specified key. Here's an example of how to use it:
**/*.tsx
复制代码
复制代码到剪贴板
import type { FC } from "react";
import { useIntlayer } from "react-intlayer";
const NestComponent: FC = () => {
const { fullNestedContent, partialNestedContent } = useIntlayer(
"key_of_my_second_dictionary"
);
return (
<div>
<p>Full Nested Content: {JSON.stringify(fullNestedContent)}</p>
<p>Partial Nested Value: {partialNestedContent}</p>
</div>
);
};
export default NestComponent;在 React Intlayer 中使用嵌套
要在 React 组件中使用嵌套内容,可以利用 react-intlayer 包中的 useIntlayer 钩子。此钩子根据指定的键检索正确的内容。以下是如何使用它的示例:
**/*.tsx
复制代码
复制代码到剪贴板
import type { FC } from "react";
import { useIntlayer } from "react-intlayer";
const NestComponent: FC = () => {
const { fullNestedContent, partialNestedContent } = useIntlayer(
"key_of_my_second_dictionary"
);
return (
<div>
<p>
Full Nested Content: {JSON.stringify(fullNestedContent)}
{/* 输出: {"content": "content", "subContent": {"contentNumber": 0, "contentString": "string"}} */}
</p>
<p>
Partial Nested Value: {partialNestedContent}
{/* 输出: 0 */}
</p>
</div>
);
};
export default NestComponent;其他资源
有关配置和使用的更多详细信息,请参考以下资源:
这些资源提供了在不同环境和各种框架中设置和使用 Intlayer 的更多见解。