0

I am trying to use typescript along with Vue (I am new at it) in my vue but having issue as it seems like it is scope problem. I might be wrong. I took small example from VueJS and made it like following

Cannot read property 'message' of null"

<template>
    <button @click="onClick">Click!</button>
</template>
<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'
export default class MyComponent extends Vue {
  // Initial data can be declared as instance properties
  message: string = 'Hello!'

  // Component methods can be declared as instance methods
  onClick (): void {
    window.alert(this.message)
  }
}
</script>

What am I missing?

0

1 Answer 1

2

You missed the component decorator before defining MyComponent:

@Component
export default class MyComponent extends Vue {

}
Sign up to request clarification or add additional context in comments.

5 Comments

oh wow. It is confusing when ur jumping from just Vue to vue typescript. Does it change more if we r using Vuex? Any good tutorial? As my script is more complicated, I still get the error. I wonder how we bind input types with properties.
Vuex with typescript was a bit of a pain to setup due to lack of tutorials. I recommend looking for example projects on github, that helped me the most.
thanks, the thing I don't understand is why some ppl use Vue.extend and some use class MyClass extends Vue
I think Vue.extend is the only option available if using javascript, so if you're moving an existing code base over to typescript then its probably easier to keep using it. Class based components are exclusive to typescript (I think) and result in cleaner and more readable code which is understandable by anyone familiar with classes (my opinion).
so One need to be well versed with OOP in order to do good TS? Just asking for recommendation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.