苏公子
苏公子
  • 发布:2025-03-25 17:25
  • 更新:2025-04-09 11:05
  • 阅读:85

多语言:nvue页面中引入vue组件,vue组件的多语言会报错,如何处理?

分类:uni-app

如题。
会报【TypeError: Cannot read property '_t' of undefined】
vue组件中做成nvue的引入方式也是不可以。
这种情况有什么解决方案?

2025-03-25 17:25 负责人:无 分享
已邀请:
苏公子

苏公子 (作者)

找到了解决方案:在引入的vue组件中使用条件编译引入i18n插件即可。

// 页面引入mixin

import locale from '@/mixin/locale.js'  
mixins: [locale]

// mixin内容

import { initVueI18n } from '@dcloudio/uni-i18n'  
import messages from '@/locale/index.js'  

const i18n = initVueI18n(messages)  

export default {  
    data() {  
        return {  
        }  
    },  
    methods: {  
        $t(key, obj) {  
            return i18n.t(key, obj)  
        },  
    }  
}

要回复问题请先登录注册