Search code examples
javascripthtmlcssvue.jsvuex

methods/computed cant access data in vue component


I have a simple vue component where i defined a boolean constant in data with start value false. I now want to create a method to change it to true and bind certain stuff conditionally to that in the template when it changes. But somehow i get the error "property value does not exist on type...". when i move computed out of data i get "Property 'computed' has no initializer"

export default class Something extends Vue {
  data() {
    return {
      value: false,
      computed: {
        valueTransform() {
          this.value = true
          alert(this.value)
        },
      },
    }
  }
}

Solution

  • This syntax is not valid in class components, you should have something like :

    export default class Something extends Vue {
     
        //data
          value = false,
         
        //computed 
           get valueTransform(){
            return this.value
          }
    }