[Vuejs]-Apply inline style background image based on a ternary operator

0👍

Just add computed property and bind it to style, make template more clean:

  computed: {
      styles() {
          if (!this.item.packageAtGradeImage.url) {
              return null;
          }
          return { 'background-image': 'url(' + this.item.packageAtGradeImage.url + ')' };
      }
  }

In template:

<div :style="styles"></div>

Leave a comment