0

I want to define a Vuetify textfield component with the rules prop

https://vuetifyjs.com/en/api/v-text-field/#props-rules

Since some of these rules rely on the current value of Vuex getters I can't define the rules inside the data function, I have to return them as a computed property.

This is a sample implementation, unfortunately I can't access the Vuex getter because this seems to have the wrong scope...

new Vue({
  el: '#app',
  methods: {
    run() {
      this.aLocalComputedProperty.forEach(func => {
        console.log(func());
      });
    }
  },
  computed: {
    mappedGetterFromVuex: () => true,
    aLocalComputedProperty: () => [
      () => "first rule is fine",
      () => {
        if (!this) {
          console.log("this is undefined");

          return null;
        } else if (!this.mappedGetterFromVuex) {
          console.log("this.mappedGetterFromVuex is undefined");

          return null;
        }

        return this.mappedGetterFromVuex;
      }
    ]
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <button @click="run">run</button>
</div>

Does someone know how to fix that?

Question3r
  • 2,166
  • 19
  • 100
  • 200

1 Answers1

1

try to use the computed prop like this.

aLocalComputedProperty: function () {
      //your code here
    }

This should be in the right scope now

moritz4004
  • 26
  • 5