You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue项目中Router调用含i18n依赖的Pinia Store启动报错的问题求助

Vue项目中Router调用含i18n依赖的Pinia Store启动报错的问题求助

我现在遇到个挺头疼的问题,想请教下各位大佬。我在Vue项目的router.ts里,需要获取Pinia的storeA里的某个值,于是就在路由守卫beforeEach里调用了useStoreA()来判断状态,代码大概是这样的:

router.beforeEach((to, from, next) => {
    if(useStoreA().myString == "something") {
       next()
    }
})

但麻烦的是,我的storeA里虽然我要用到的myString和i18n没关系,但其他状态用到了vue-i18n的翻译函数t,Store的代码是这样的:

import { useI18n } from "vue-i18n";

export const useStoreA: StoreDefinition<"storeA"> = defineStore(
  "storeA",
  () => {
    const t = useI18n().t;
    const myString = "something";
    const myOtherString = t("hey");
    
    return { myString, myOtherString }
  }
)

结果项目启动的时候直接触发了Vue Router的警告,报错信息是:

[Vue Router warn]: Unexpected error when starting the router: SyntaxError: Must be called at the top of a...

备注:内容来源于stack exchange,提问作者Nicolas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 13:29:31