Search code examples
vue.jsternaryprop

How to pass a prop with a default value in case the prop is empty without using the prop's "default" property


I'm using a third party multi-language package where translation values can only be obtained/used from component's template, not from component's logic (at least I can't find how to the latter).

I need to pass such translation value in some component as a default prop value:

:placeholder="{ propValue ? propValue : $t('value') }

If placeholder is explicitly specified then use that. If not, use $t('value') instead. What's the right way to write this?

I tried this in reaction to @Michael's answer:

import i18n from "@/utilities/i18n";
export default {
  data() {
    return {
      test: i18n.t('register.agreeTermsCaptionLink')
    };
  }
};

Getting this error:

_utilities_i18n__WEBPACK_IMPORTED_MODULE_7__.default.t is not a function


Solution

  • You can set default value to prop like this:

    props: {
         propValue: {
             type: String,
             default: this.$t('value')
         }
    }
    

    With that in your template you need just to assign that value like: :placeholder="propValue"

    Is that what you trying to achive?

    Good luck!