尝试使用组合api在组件中实现vue-i18n。我希望能够在onMounted钩子中设置一些翻译消息。在options api中,我将使用this.$i18n.setLocaleMessage(locale, messages)
。
但是,this
在组合apis的Setup()方法中不可用。因此,当我尝试上面的方法时,它给我带来了不确定性。我可以通过将i18n导入到component:import { useI18n } from 'vue-i18n'
中,然后将其创建为var i18n = useI18n({}), i18n.setLocaleMessage()
实例来实现,但我更喜欢像第一个解决方案那样的单行解决方案。
发布于 2021-09-08 12:58:47
只需使用useI18n
中的t
,如下所示:
const {t} = useI18n({})
//then use in any place in the setup hook
onMounted(()=>console.log(t('someWord'))
https://stackoverflow.com/questions/69103550
复制相似问题