[Vuejs]-Global method this.$t from vue-i18n does not work in function

3đź‘Ť

âś…

convertErrors has no “this”, it’s not bound to a component. Move it into the component’s methods in order to reference this:

methods: {
  convertErrors(jsonErrors) {
    const veeErrors = {};
    console.log(this.$t('sign-up.heading'));
    return veeErrors;
  }
}

OR

use call in order to set the context (the this) inside convertErrors:

const veeErrors = convertErrors.call(this, error.response.data);
👤Ross Allen

0đź‘Ť

You can store VueComponent before for each loop. Like this one:

let self = this
// .......
jsonErrors.errors.forEach((error) => {
  if (error.field) {
    veeErrors.$field = [self.$t(error.messageKey)];
  } else {
    self.error = self.$t(error.messageKey);
  }
});
👤glinda93

Leave a comment