• Vue3中使用i18n,this.$t报错


    方案一 

    //需要把$t手动挂载到全局 

    1. //main.js
    2. app.config.globalProperties.$t = i18n.global.t
    1. //需要使用的时候在组件里引用
    2. import { getCurrentInstance } from 'vue'
    3. const _this = getCurrentInstance().appContext.config.globalProperties
    4. console.log('$i18n',_this.$t('欢迎'))

    方案二

    legacy设置为false

    1. //main.js
    2. const i18n = createI18n({
    3. locale: localStorage.getItem('i18n-lang'),
    4. fallbackLocale: 'zh',
    5. messages: {
    6. en,
    7. zh
    8. },
    9. legacy: false
    10. })
    1. //组件中先引用再使用
    2. import { useI18n } from 'vue-i18n'
    3. const { t } = useI18n()
    4. console.log('$i18n',t("欢迎"))

    方案三

    直接像vue2一样,使用选项式写法,可以直接使用

    1. //使用vue选项式写法,可以直接使用this.$t

  • 相关阅读:
    Spring Security(1)
    SAP Router 配置手册
    PMP证书续证流程
    React-hook-form-mui(五):包含内嵌表单元素的表单
    计算机网络(自顶向下方法)-应用层
    CY3 CY5 CY7 FITC荧光素标记果糖/鼠李糖/水苏四糖
    2022/11/28-29总结
    clip-path属性深入理解与使用
    @ConditionalOnProperty 用法
    Java培训:自定义代码生成器
  • 原文地址:https://blog.csdn.net/u010741500/article/details/138152143