[Vuejs]-Make computed property available globally

0👍

You can try creating a Mixins file and add the computed property in it.

Mixin named test-mixin

export default {
  computed: {
    filteredPlans: {
      get() {
        let res = [];
        if (this.query.length > 1) {
          this.plans.forEach(p => {
            if (JSON.stringify(p).includes(this.query)) res.push(p);
          });
          return res;
        } else return this.plans;
      },
      set() {
        return this.plans;
      }
    }
  },
}

This can be reused in any component by importing the mixin file like follows

import testMixin from 'test-mixin';
export default {
  mixins: [testMixin]
}

Leave a comment