vue-i18n: `$t` undefined in `.vue` file
vue & vue-i18n version
ex: 2.1.4, 5.0.1
Steps to reproduce
Everything works fine if I define the component directly:
Vue.component('form-builder', {
mounted: function() {
console.log(this.$t); // this will output the function's source code
}
});
but if I change this to
Vue.component('form-builder', require('./test.vue'));
and the content of the test.vue is
<template>
<div>
</div>
</template>
<style>
</style>
<script>
export default {
mounted: function() {
console.log(this.$t); // it says undefined
},
}
</script>
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 15 (4 by maintainers)
I solved the problem by using
this.$i18n.t()to translate strings in mydatafunction. Maybe this is useful for the others who are having similar issues.this.$t is not a function this same issue here.
i18n v6.0.0 -alpha.1
Same issue here: node 7.5.0 - latest version of dependencies.
this.$tisundefinedin dataUncaught TypeError: this.$t is not a functionI had a problem in mixin:
I fixed the problem by using computed field:
The reason behind this is $t is not available when your app is being loaded, that is why you can’t use it in data() as $t(‘xx’), you however have two option here, either you update your variables after your app has been loaded inside mounted method
mounted() { myProp = this.i18n.t('xx_str') },or you can declare your variables in computed block instead of data block. Just like the answer above.
related: https://github.com/kazupon/vue-i18n/issues/119#issuecomment-284198547
sorry, I’ll try to write to docs later.
@kazupon I don’t know the detail of this issue, could you help me to ask them?