我得到一个翻译后的json文件,
const skillsJson = require("./locales/" + localStorage.lang + "/skills.json");
,当我使用npm run serve运行它时,它工作得很好。
但是在使用npm run build构建项目之后,我在控制台中收到此错误:
Uncaught Error: Cannot find module './undefined/skills.json'
at i (skills\.json$:13)
at s (skills\.json$:8)
at Module.56d7 (App.vue:225)
at r (bootstrap:79)
at Object.0 (app.5886208a.js:1)
at r (bootstrap:79)
at n (bootstrap:45)
at bootstrap:152
at app.5886208a.js:1
好像找不到本地语言,而后备语言却无效。但是我认为我的i18n.js文件很好。在这里:
import Vue from "vue";
import VueI18n from "vue-i18n";
const moment = require("moment");
Vue.use(VueI18n);
function loadLocaleMessages() {
const locales = require.context(
"./locales",
true,
/[A-Za-z0-9-_,\s]+\.json$/i
);
const messages = {};
locales.keys().forEach(key => {
const matched = key.match(/([A-Za-z0-9-_]+)\./i);
if (matched && matched.length > 1) {
const locale = matched[1];
messages[locale] = locales(key);
}
});
return messages;
}
let lang = "de";
if (localStorage.getItem("lang")) {
lang = localStorage.getItem("lang");
} else {
localStorage.setItem("lang", lang);
moment.locale(localStorage.getItem("lang"));
}
export default new VueI18n({
locale: lang,
fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || "de",
messages: loadLocaleMessages()
});
有人猜测可能是什么问题?如果您需要任何其他信息,请告诉我。