Vue3中使用i18n;Vue3中使用$t;$t获取不到;vue3中如果获取/使用原型链中的方法

vue3中如果获取/使用原型链中的方法

template中

//与vue2一致  
    <div > 
        {
    
    {
    
    $t("back")}}
    </div>

setup中

    import {
    
     ref,getCurrentInstance } from "vue"; 
    const _this = getCurrentInstance().appContext.config.globalProperties  
	// 此处可打印_this查看是否存在$t方法 如果$t方法不存在 
	//检查一下i18n配置globalInjection: true是否发开如果打开了仍无效 查看下方代码
	_this.$t("game.INSTALL")

手动将$t挂载至全局

// 此段代码写入main.js中
app.config.globalProperties.$t = i18n.global.t

猜你喜欢

转载自blog.csdn.net/lys20000913/article/details/127807881