我想在脚本谷歌地图中设置区域设置,如下所示:
<script defer src={`https://maps.googleapis.com/maps/api/js?key=key&libraries=places&language=${locale}`}/>
Run Code Online (Sandbox Code Playgroud)
但我怎样才能到达文档 nextJS 中的区域设置?useRouter 在文档组件中不起作用。
export default function Document() {
return (
<Html>
<Head>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<script defer src={`https://maps.googleapis.com/maps/api/js?key=key&libraries=places&language=${locale}`}/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
Run Code Online (Sandbox Code Playgroud)
您可以访问 中的区域设置this.props.locale。
在我的项目中,该区域设置在这种情况下使用:
<Html lang={this.props.locale}>
/....
Run Code Online (Sandbox Code Playgroud)
我在 _document 中使用类组件。
在您的变体中,您可以仅在 props 中访问语言环境。
import { Html, Head, Main, NextScript } from "next/document";
export default function Document(props: DocumentProps) {
return (
<Html lang={props.locale}>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
Run Code Online (Sandbox Code Playgroud)
如果您已经添加 i18n 配置并将其注入到下一个配置中,它就可以工作。
| 归档时间: |
|
| 查看次数: |
1126 次 |
| 最近记录: |