reactjs - 在我的 App 组件中导入 i18n 后,组件消失了
问题描述
如纪录片中所述创建 i18n.js 文件,并在文件中插入以下代码
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import Backend from 'i18next-http-backend';
import LanguageDetector from 'i18next-browser-languagedetector';
const languages = ["en", "es", "ar"]
const options = {
order: [ 'localStorage', 'cookie', 'querystring', , 'sessionStorage', 'navigator', 'htmlTag', 'path', 'subdomain'],
lookupQuerystring: 'lng',
lookupCookie: 'i18next',
lookupLocalStorage: 'i18nextLng',
lookupSessionStorage: 'i18nextLng',
lookupFromPathIndex: 0,
lookupFromSubdomainIndex: 0,
caches: ['localStorage', 'cookie'],
excludeCacheFor: ['cimode'],
cookieMinutes: 10,
cookieDomain: 'myDomain',
htmlTag: document.documentElement,
cookieOptions: { path: '/', sameSite: 'strict' }
}
i18n
.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
fallbackLng: 'en',
lng: 'en',
debug: true,
whitelist: languages,
detection: options,
interpolation: {
escapeValue: false,
}
});
export default i18n;
然后在 App 组件中导入上述文件,刷新已经翻译的页面后,组件正在消失,但是当我评论 i18n.js 在我的 App 组件中导入的文件时,一切正常
解决方案
请检查浏览器控制台,您可能会在那里找到答案。
快速查看您的代码,您的对象似乎options
在数组中存在类型错误order
,即额外的逗号。
..., 'querystring', , 'sessionStorage', ...
我建议使用 eslint,以便您可以在 IDE 中找到并解决此问题。
推荐阅读
- android - 解析复杂 json 数据时出现 Flutter 问题
- python - 根据子字符串在字符串中的位置对子字符串列表进行排序
- junit - 如何将日期的 Junit 测试用例编写为字符串格式化程序方法?
- python - 如何使用布尔索引索引熊猫数据框?
- r - 在 R 和 ggplot2 中绘制正态分布的垂直密度
- swift - 将 CIImage 调整为精确大小
- c# - “{”后使用的类型“x”必须是标记扩展。错误代码 0x09c6
- sqlite - Delphi 中的 SQLite 错误地显示单个 unicode 字符字段
- javascript - 如何有多个输入文件?
- node.js - git push 到远程不会终止