nuxt3在setup外部引用i18n的t函数的正确方法

目的是直接封装一个$t函数,可以直接在script中使用。

如果直接这样写,会报错 “Must be called at the top of a `setup`”

 

function $t(args){
  const {t} = useI18n()
  t(args) }

 

 

所以可以这样写:

function $t(args1: any,args2?:any, args3?:any){
    const { $i18n } = useNuxtApp()
    return $i18n.t(args1, args2, args3)
}

 

posted @ 2024-07-10 17:04  alpiny  阅读(10)  评论(0编辑  收藏  举报