Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write computed setters in class-based components in vuejs

I have below code for a computed property.

computed: {
  filterText: {
    get() {
      return this.filter; // it's a vuex state
    },
    set(value) {
      this.setFilter(value); // it's a vuex action
    }
  }
}

Now, I want to write it in class-based components. I think getter should be like this, but how to write setter?

get filterText() {
  return this.filter
}
like image 244
39ecneret Avatar asked Dec 21 '18 07:12

39ecneret


1 Answers

Class based components use get and set for computed properties:

get filterText() {
  return this.filter
}

set filterText(value) {
  this.filter = value
}

A single file component written in TypeScript would be structured like:

<script lang="ts">
  import { Component, Vue } from 'vue-property-decorator'

  @Component
  export default class MyClass extends Vue {
    private filter: string = ''

    public get filterText(): string {
      return this.filter
    }

    public set filterText(filter: string) {
      this.filter = filter
    }
  }
</script>
like image 92
Brian Lee Avatar answered Oct 10 '22 16:10

Brian Lee