原因:
在组件外使用pinia
了,所以导致没有正确缓存
如果正确缓存,控制台会打印store
安装完毕的提示
我在项目使用了i18n
,导致在router.beforeEach
之前使用pinia
了(服务端使用),也就是官网所说的,组件外使用pinia
。
具体代码如下:
// i18n.ts
import { createI18n } from "vue-i18n";
import zhCN from "@/lang/modules/zhCN";
import enUS from "@/lang/modules/enUS";
// 在组件外使用了pinia
import pinia from "@/store/index";
import { storeToRefs } from "pinia";
import { useThemeConfig } from "@/store/modules/theme-config";
const themeStore = useThemeConfig(pinia);
const { language } = storeToRefs(themeStore);
/* 这里必须是messages名称 */
const messages = {
"zh-CN": zhCN,
"en-US": enUS
};
const i18n = createI18n({
legacy: false, // Composition API模式需要设为false
globalInjection: true, // 全局生效: $
locale: language.value, // 默认语言
messages // 数据源
});
export default i18n;
解决:
如果组件外不使用pinia,store
就可以被缓存
import { createI18n } from "vue-i18n";
import zhCN from "@/lang/modules/zhCN";
import enUS from "@/lang/modules/enUS";
/* 获取语言-直接从本地获取,为使用pinia */
const getLang = () => {
let store = localStorage.getItem("theme-config");
if (store) {
return JSON.parse(store)?.language || "zh-CN";
} else {
return "zh-CN";
}
};
/* 这里必须是messages名称 */
const messages = {
"zh-CN": zhCN,
"en-US": enUS
};
const i18n = createI18n({
legacy: false, // Composition API模式需要设为false
globalInjection: true, // 全局生效: $
locale: getLang(), // 默认语言
messages // 数据源
});
export default i18n;
参考资料:
组件外使用store
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。