首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加gatsby插件布局后引起错误的injectIntl

添加gatsby插件布局后引起错误的injectIntl
EN

Stack Overflow用户
提问于 2020-05-02 17:53:43
回答 1查看 609关注 0票数 0

为了翻译我的站点,我在我的intl文件中注入了这样的layout.js:

代码语言:javascript
复制
import React from "react";
import { injectIntl } from "gatsby-plugin-intl";

const Layout = ({intl}) => (
    {intl.formatMessage({id: "history_text"})}
);

export default injectIntl(Layout)

但是,在我将gatsby-plugin-layout添加到我的项目(基于这个例子)之后,我得到了以下错误:

Error: [React Intl] Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry.

如何在保留翻译的同时消除此错误?

这是相关的gatsby配置部分:

代码语言:javascript
复制
{
    resolve: `gatsby-plugin-intl`,
    options: {
        path: `${__dirname}/src/locale`,
        languages: [`en`, `de`],
        defaultLanguage: `de`,
        redirect: false,
    },
},
{
    resolve: `gatsby-plugin-layout`,
    options: {
        component: require.resolve(`./src/components/layout.js`),
    },
},
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-02 18:34:44

gatsby-plugin-layoutgatsby-plugin-intl都使用wrapPageElement API来创建包装器。

现在,gatsby中的插件是自上而下执行的,因此您需要在gatsby-plugin-layout之前定义gatsby-plugin-intl,以便gatsby-plugin-intl使用的IntlProvider提供程序封装布局组件,并且它可以使用injectIntl自定义。

代码语言:javascript
复制
{
    resolve: `gatsby-plugin-layout`,
    options: {
        component: require.resolve(`./src/components/layout.js`),
    },
},
{
    resolve: `gatsby-plugin-intl`,
    options: {
        path: `${__dirname}/src/locale`,
        languages: [`en`, `de`],
        defaultLanguage: `de`,
        redirect: false,
    },
},
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61563906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档