vuejs2vue-componentvue-filter

dynamic variable combining a double value + filter


I have the following case:

<bl-card :subtitle="card.beforeChanged + ' ' + (card.changed | formatDate)" />

The subtitle needs to be set by combining two strings. card.beforeChanged contains "last changed to:" string, the card.changed variable contains a datetimestring. Via de formatDate() the datetimestring gets formatted to a readable date.

subtitle now returns: "last changed to: 2069882880".

The question: is it possible to combine two strings where one of them get's formatted via the filter property in one go?


Solution

  • Got it working through a method. Computed properties weren't an option since the datestring comes from within a for loop in the template.

    Method:

    formatDate: (date, format) => {
      if (!date) return ''
      if (!format) format = 'DD.MM.YYYY'
      return moment(date).format(format)
    }
    

    Implementation:

    <bl-column v-for="(card, index) in cards" :key="card.id">
         <bl-card :index="index" type="section" action="drawer" :title="card.titleShort" :subtitle="(card.beforeChanged || '') + ' ' + formatDate(card.changed)" />
    </bl-column>