Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 即使声明了名称空间,也要保持名称空间是必需的_Reactjs_Next.js_Next I18next - Fatal编程技术网

Reactjs 即使声明了名称空间,也要保持名称空间是必需的

Reactjs 即使声明了名称空间,也要保持名称空间是必需的,reactjs,next.js,next-i18next,Reactjs,Next.js,Next I18next,我已经用我的NextJS和React项目设置了next-18next,翻译在我的项目中非常有效 但我一直有以下著名的警告: 您尚未在页面级别声明名称空间所需的数组 组件:带有i18nextTranslation(索引)。这将导致所有 要向下发送到客户端的名称空间,可能是负面的 影响应用程序的性能。有关详细信息,请参阅: 我已经在我的每个页面级组件中声明了所需的名称空间(404页面除外,因为我在文档中读到了您无法访问的内容) 但是: 我只有Functional组件,所以我使用了getStati

我已经用我的NextJS和React项目设置了next-18next,翻译在我的项目中非常有效

但我一直有以下著名的警告:

您尚未在页面级别声明名称空间所需的数组 组件:带有i18nextTranslation(索引)。这将导致所有 要向下发送到客户端的名称空间,可能是负面的 影响应用程序的性能。有关详细信息,请参阅:

我已经在我的每个页面级组件中声明了所需的名称空间(404页面除外,因为我在文档中读到了您无法访问的内容)

但是:

  • 我只有Functional组件,所以我使用了getStaticProps和 有翻译

  • 我读到过,如果你没有一个 但我有一个(它出现在选项卡中)

不知道现在去哪里看。。。以下是触发警告的页面代码

import React from "react";
import IndexView from "../shared/views/index/index"
import i18nextInstance from "../i18n"

function Index() {
  return (
    <IndexView />
  );
};

Index.getInitialProps = async () => ({
  namespacesRequired: ["common"]
})

export default i18nextInstance.withTranslation("common")(Index);
myserver.js

const express = require('express')
const next = require('next')
const nextI18NextMiddleware = require('next-i18next/middleware').default
const nextI18next = require("./i18n")


const port = process.env.PORT || 3000
const app = next({ dev: process.env.NODE_ENV !== 'production' })
const handle = app.getRequestHandler();

(async () => {
    await app.prepare()
    const server = express()

    await nextI18next.initPromise
    server.use(nextI18NextMiddleware(nextI18next))

    server.get('*', (req, res) => handle(req, res))

    await server.listen(port)
    console.log(`> Ready on http://localhost:${port}`) // eslint-disable-line no-console
})()
最后,my_app.js将此作为导出行

export default nextI18next.appWithTranslation(App)

如果你们有什么想法,我真的很高兴听到:)谢谢。

我没有使用server.js,因为我忘了告诉我的package.json启动它

"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js"
现在错误消失了

"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js"